Does anyone know a good way to find out what MediaPlayer
is currently playing and stop/pause it using the onStop()
or onPause()
override? Or just stop MediaPlayer
when the app is in the background? I have lots of sounds and I don't know which will be playing at any exact moment. Sorry, still learning here.
Asked
Active
Viewed 1,894 times
5
-
possible duplicate of [Connect to Music app on Android](http://stackoverflow.com/questions/5522409/connect-to-music-app-on-android) – CommonsWare Apr 03 '11 at 11:47
-
still was unable to figure out how to do this. Right now I have a huge else if going on at the end of my Java. – Colby Apr 04 '11 at 18:37
-
Both questions have been answered on this site: [detect activity in background](http://stackoverflow.com/q/3667022/741249) and [stop mediaplayer](http://stackoverflow.com/q/5470068/741249) If you have multiple mediaplayers I suggest you keep track of the active one(s) in a common class, for example in the [Application class](http://developer.android.com/reference/android/app/Application.html) if you have one – THelper Jan 26 '12 at 15:26
3 Answers
0
Use this to stop the mediaPlayer when the activity finishes.
@Override
protected void onStop() {
super.onStop();
mediaPlayer.stop();
mediaPlayer.release();
}

Munawir
- 3,346
- 9
- 33
- 51
0
You can stop
and release
the mediaPlayer
@Override
protected void onStop() {
mediaPlayer.stop();
mediaPlayer.release();
}
0
You can check if a MediaPlayer is null and stop it if it's not. You could do this for all your MediaPlayers in a Stop button listener or onBackPressed()
if (myPlayer != null){
myPlayer.stop();
}

Carnivoris
- 793
- 3
- 7
- 23