I can stop the Audio but if I check my Android-Studio Debug windows, i see that the MediaPlayer works in Background !!!
I play the Sound like that :
try {
myMediaPlayer1 = new MediaPlayer();
myMediaPlayer1.setAudioStreamType(AudioManager.STREAM_MUSIC);
myMediaPlayer1.setDataSource("http://myweb.com/audios/1.mp3");
myMediaPlayer1.prepare();
myMediaPlayer1.start();
} catch (Exception e) {
// TODO: handle exception
}
I stop the MediaPlayer via a click on a Button or OnDestry like that :
@Override
protected void onDestroy() {
super.onDestroy();
myMediaPlayer1.stop();
}
I debug via USB Connection. After Stop the Sound I see the MediaPlay works in Background :
You need to release media player using mediaPlayer.release()
. If MediaPlayer
is not playing any song/audio you shouldn't stop MediaPlayer
. See below code for more help.
private void releaseMediaPlayer() {
try {
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying())
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}