所以我有一个播放所选文件的按钮。但是,当再次按下按钮时,我想暂停播放。我查看了一些 StackOverflow 帖子和教程,大多数情况下都使用了 pause 方法。但是它对我不起作用。音频继续播放。我错过了什么还是我的代码有问题?任何帮助表示赞赏。谢谢!
buttonPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String fileName = getFileSelected();
MediaPlayer m = new MediaPlayer();
if ( !m.isPlaying() && (getFileSelected().endsWith(".3gp") || getFileSelected().endsWith(".wav"))) {
try {
m.setDataSource(getActivity().getApplicationContext().getExternalFilesDir(null).toString() + File.separator + fileName);
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
m.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
isPlaying = true;
buttonPlay.setText("Stop");
m.start();
Toast.makeText(getActivity().getApplicationContext(), "Playing audio", Toast.LENGTH_LONG).show();
}
else{
// m.stop();
// m.release();
// m = null;
m.pause();
isPlaying = false;
setButtonLabel(R.id.btnPlay, "Play Selected Recording");
}
}
});