简体   繁体   English

Android:如何知道 MediaPlayer 是否暂停?

[英]Android : how to know if MediaPlayer is paused?

MediaPlayer.isPlaying() does not allow to know if the MediaPlayer is stopped or paused. MediaPlayer.isPlaying()不允许知道MediaPlayer是停止还是暂停。 How to know if it is paused and not stopped?如何知道它是暂停了还是没有停止?

Thanks !谢谢 !

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). 一种方法是检查媒体播放器是否播放(暂停)并检查它是否处于起始位置(1)以外的位置。

MediaPlayer mediaPlayer = new MediaPlayer();

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

There is no API to check if MediaPlayer is paused or not. 没有用于检查MediaPlayer是否暂停的API。 So please use any Boolean variable to check and toggle it when you paused using any button . 因此,请在使用任何button暂停时使用任何布尔变量进行检查和切换。

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

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. 定义一个名为playedAtLeastOnce (或任何你想要的)的boolean变量,如果你的MediaPlayer对象至少播放过一次,则将其设置为true A good place to assign it to true is in onPrepared(MediaPlayer mp) method from MediaPlayer.OnPreparedListener implemented interface. 将其赋值为true的好地方是来自MediaPlayer.OnPreparedListener实现的接口的onPrepared(MediaPlayer mp)方法。

Then if MediaPlayer is not playing and playedAtLeastOnce is true , you can say that MedaiPlayer is paused. 然后,如果MediaPlayer没有播放并且playedAtLeastOncetrue ,则可以说MedaiPlayer已暂停。

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

// You can create global variable private boolean notIdle = false; // 你可以创建全局变量 private boolean notIdle = false;

// After you setDataSource(), in the listener setOnPreparedListener() notIdle = true; // 在你 setDataSource() 之后,在监听器中 setOnPreparedListener() notIdle = true;

// Override MediaPlayer.reset() funtion, inside it notIdle = false; // 覆盖 MediaPlayer.reset() 函数,在它里面 notIdle = false;

// notIdle will be true in start(), pause() and stop() states... you can change this behaviour as you consider more convinient. // notIdle 将在 start()、pause() 和 stop() 状态下为真...您可以在您认为更方便时更改此行为。

As Ankit answered there is no API to check whether Media Player is paused or not ,we have to check it programmatically like this 正如Ankit所回答的,没有用于检查Media Player是否暂停的API,我们必须以编程方式检查它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM