Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53647

How to stop media player properly , android

I have created a list of songs on click on the song i am able to play the song using MedaiPlayer. While one song is playing if the user clicks another song then i am stopping the media player and starting the player again. But I am getting illegalstateexception in reset(). Here is the code where I am getting the exception. How to stop a player properly? also why am i getting this exception. How to avoid it?

public void stopPlayer() {
        try {
            if (player != null) {
                // Log.e("Trying to Stop "," Player ");
                player.stop();
                player.release();

                player.reset();// causes IllegalstateException

                player = null;

            }

        } catch (Exception e) {
            player = null;
            playerStatus = false;
            e.printStackTrace();
        }
    }

Upvotes: 8

Views: 14352

Answers (5)

Hardzsi
Hardzsi

Reputation: 41

The Media Player State Diagram shows, and also states:

Calling stop() stops playback and causes a MediaPlayer in the Started, Paused, Prepared or PlaybackCompleted state to enter the Stopped state.

  • Once in the Stopped state, playback cannot be started until prepare() or prepareAsync() are called to set the MediaPlayer object to the Prepared state again.

That means, that after calling stop(), we should call prepare() on the same audio file if we wish to play it again. Otherwise calling start() again won't do anything.

As prepare() might throw exception, we should wrap it in a try-catch block, like this:

    public void stopAudio(View view) {
    mplayer.stop();
    try {
        mplayer.prepare();
    } catch (IOException e) {
        Log.e("stopAudio", "Unable to prepare() mplayer after stop()", e);
    }
}

Upvotes: 0

Devrath
Devrath

Reputation: 42824

Though the accepted answer works, This is a better way to achieve the task

private void stopSong() {

        if(mediaPlayer!=null) {
            if(mediaPlayer.isPlaying()) {
                mediaPlayer.reset();// It requires again setDataSource for player object.
                mediaPlayer.stop();// Stop it
                mediaPlayer.release();// Release it
                mediaPlayer = null; // Initialize it to null so it can be used later
            }
        }

    }

Upvotes: 4

Ramesh Akula
Ramesh Akula

Reputation: 5740

If you want to play again ,then use player.reset(), player.release() means that it releases the player object so you have to re-intialise the player. So first you use reset() and then release(). release() is used when your player object no longer working. When your activity destroys release() method to be used for good practice.

Whenever you want to stop it:

if(player!=null)
{
   if(player.isPlaying())
      player.stop();

   player.reset();//It requires again setDataSource for player object.

}

Whenever your player no longer to be needed:

if(player!=null)
{
   if(player.isPlaying())
      player.stop();

   player.reset();//It requires again setDataSource for player object.
   player.release();
   player=null; // fixed typo.

}

Upvotes: 6

Pawan
Pawan

Reputation: 1523

try this :

player.reset();
player.release();

and also have a look at media player state diagram.

Upvotes: 10

Jason Robinson
Jason Robinson

Reputation: 31283

Are you planning on reusing the player again, or are you done with the player? If you're done with the player, call release() and not reset(). If you plan on reusing the player, call reset() and not release().

reset() resets the player to its uninitialized state. release() frees all resources associated with the player.

Upvotes: 1

Related Questions