I want to play a sound when onPause()
of my Activity is called. To do so, I use the following code:
@Override
public void onPause()
{
super.onPause();
MediaPlayer player = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);
player.setLooping(false);
player.start();
}
This works fine, but the sound keeps on playing forever. I tried adding an OnCompletionListener
, but that didn't help (the Listener
is never called).
I know that I'm not calling player.stop()
and player.release()
anywhere, but I wouldn't even know where to do so.
My question is now: How can I make the sound play only once? Is there a way to wait for the media player to finish before the application goes to the background?