How to stop media player properly , android

家住魔仙堡 提交于 2019-12-07 01:29:10

问题


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();
        }
    }

回答1:


try this :

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

and also have a look at media player state diagram.




回答2:


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.

}



回答3:


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
            }
        }

    }



回答4:


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.




回答5:


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);
    }
}


来源:https://stackoverflow.com/questions/8223908/how-to-stop-media-player-properly-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!