So I have a button that plays the selected file. However I want to pause the play when the button is pressed again. I checked a few StackOverflow posts and tutorials and the pause method is what was used in most cases. However it is not working for me. The audio keeps on playing. Am I missing something or is there a problem with my code? Any help is appreciated. Thanks!
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");
}
}
});