Skip to content Skip to sidebar Skip to footer

Releasing Mediaplayer And Stopping It OnPause And OnResume Gives Error In Android

i m using videoView and mediaplayer but stopping mediaplayer in onPause and onResume gives me error: static MediaPlayer mediaPlayer; private VideoViewCustom videoView; @Override

Solution 1:

According to the Android documentation:

"IllegalStateException if the internal player engine has not been initialized or has been released."

So ensure your MediaPlayer is initialized, and you don't use the released one. Use like below in onPause() and onDestroy(),

try{
    if(mediaPlayer !=null && mediaPlayer.isPlaying()){
       Log.d("TAG------->", "player is running");
       mediaPlayer.stop();
       Log.d("Tag------->", "player is stopped");
       mediaPlayer.release();
       Log.d("TAG------->", "player is released");
    }
}catch(Exception e){
}

Solution 2:

According to official documentation you MUST avoid calling stop() in IDLE, INITIALIZED or ERROR states of MediaPlayer.

release() and reset() API can be called in any state of MediaPlayer.

Unfortunately to find out current state you have to track it yourself (there is no API for that). In one of my project I create class which extend MediaPlayer and by overriding some of MediaPlayer API track it's current state, so I can easily check current state before invoking any API. If you invoke API in incorrect state usually state changes to ERROR and you have to re-initialize media player again to fix it.


Solution 3:

Try Using this...u need to check for null and u cant Stop a Non Playing Media so check for Playing too before you release or Stop

    @Override
    public void onPause()
    {
        if(mediaPlayer != null && mediaPlayer.isPlaying())
        {
            mediaPlayer.stop();
        }
        super.onPause();
    }

Solution 4:

before realeasing mediaplayer . use this line of code

mVideoView.setVisibility(View.GONE);

Here an example .

try{
    video.setVisibility(View.GONE);
    mp.reset();
    mp.release();
    mp = null;                       
    }catch(Exception e){
    Log.d("Releasing mp", e.toString());
    }

Post a Comment for "Releasing Mediaplayer And Stopping It OnPause And OnResume Gives Error In Android"