I have a MediaPlayer that plays music starting in the main (launcher) activity. I have stumbled across two problems.
When I press the home button or my app loses focus in general, the music is still playing.
When I return to the main (launcher) activity the mediaPlayer starts again (creates new mediaPlayer) and the result is that there are two MediaPlayers playing the same file simultaneously.
*For the first problem I have tried to stop the music in onStop()
method but the music stops when I go from main to other activities which is something I don't want and onDestroy
doesn't work.
My code:
if (player == null) {
player = MediaPlayer.create(this, R.raw.music);
player.setLooping(true);
if (!player.isPlaying()) {
player.start();
}
}
P.S I want the music to playing not only in main but on the other activities too.