Search code examples
javaandroidmedia-player

MediaPlayer carries on playing after app is closed


I know this is part of the multitasking functionality of android but I can't find the solution to my problem.

I have a media player object in my app that play an audio clip which is looped over and over and is supposed to only stop when the app is closed, it is running alongside a VideoView which is playing a video with no sound (reason: I need the video to loop seamlessly and only seems to do this without the sound attached to the video). I need the sound to stop when the video stops (i.e. when the app is closed. I know the VideoView has the method isPlaying() so I guess it could have something to do with that but everything I have tried so far failed.

Will I need to use a thread? Is there any way to send out a message from the VideoView saying it is no longer playing? then I could call the stop() method on the MediaPlayer object.


Solution

  • try the following code to stop the MediaPlayer Object,

    @Override
        protected void onPause() {
            if(mPlayer!=null || mPlayer.isPlaying()){
                mPlayer.stop();
                mPlayer.release();
            }
            mPlayer = null;
            super.onPause();
        }