Android : how to know if MediaPlayer is paused?

百般思念 提交于 2019-12-08 14:43:38

问题


MediaPlayer.isPlaying() does not allow to know if the MediaPlayer is stopped or paused. How to know if it is paused and not stopped?

Thanks !


回答1:


One way to do this is to check if the media player it not playing (paused) and check if it is at a position other than the starting position (1).

MediaPlayer mediaPlayer = new MediaPlayer();

Boolean isPaused = !mediaPlayer.isPlaying() && mediaPlayer.getCurrentPosition() > 1;



回答2:


There is no API to check if MediaPlayer is paused or not. So please use any Boolean variable to check and toggle it when you paused using any button.

onClick(){
 if(isPaused)
  {
    //resume media player
    isPaused = false;
  }else{
   // pause it
     isPaused = true;
  }
} 



回答3:


Define a boolean variable called playedAtLeastOnce (or whatever you want) then set it to true if your MediaPlayer object has been played at least for one time. A good place to assign it to true is in onPrepared(MediaPlayer mp) method from MediaPlayer.OnPreparedListener implemented interface.

Then if MediaPlayer is not playing and playedAtLeastOnce is true, you can say that MedaiPlayer is paused.

boolean playedAtLeastOnce;

@Override
public void onPrepared(MediaPlayer mp) {
    mp.start();
    playedAtLeastOnce = true;
}

public boolean isPaused() {
    if (!mMediaPlayer.isPlaying && playedAtLeastOnce)
        return true;
    else
        return false;
    // or you can do it like this
    // return !mMediaPlayer.isPlaying && playedAtLeastOnce;
}



回答4:


As Ankit answered there is no API to check whether Media Player is paused or not ,we have to check it programmatically like this

private boolean pause=true; //A Boolean variable to check your state declare it true or false here depends upon your requirement

Now you can make a method here to check that whenever you want to check player is pause

public void checkplayer(MediaPlayer player){
if(isPause=true)   //check your declared state if you set false or true here
Player.pause();
else if(isPause=false){
Player.play();
 or 
Player.stop();
}


来源:https://stackoverflow.com/questions/22167731/android-how-to-know-if-mediaplayer-is-paused

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