MediaPlayer player = null;
public static void playVoice(String filepath,boolean is_local){
try{
player = new MediaPlayer();
if (!is_local) {//是否为本地录音文件
AssetFileDescriptor fd = getActivity().getAssets().openFd();//access目录下录音名称即文件名
player.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
} else {
player.setDataSource(filepath);//本地录音全路径
}
player.setVolume(1, 1);
if (!player.isPlaying()) {
player.prepareAsync();
}
// 当 MediaPlayer调用prepare()方法时触发该监听器。
player.setonPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
//为 Media Player的播放完成事件绑定事件监听器。
player.setonCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// Todo Auto-generated method stub
}
});
}catch(Exception e){
e.printstacktrace();
}
}
//停止播放
public void stopPlay() {
if (player != null) {
try {
player.stop();
} catch (Exception e) {
e.printstacktrace();
player = null;
player = new MediaPlayer();
player.stop();
}
player.release();
player = null;
}
}