Java Android:如何知道 MediaPlayer 是否已暂停?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22167731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android : how to know if MediaPlayer is paused?
提问by Regis_AG
MediaPlayer.isPlaying()
does not allow to know if the MediaPlayer
is stopped or paused. How to know if it is paused and not stopped?
MediaPlayer.isPlaying()
不允许知道MediaPlayer
是停止还是暂停。如何知道它是暂停而不是停止?
Thanks !
谢谢 !
回答by Ankit
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
.
没有 API 来检查是否MediaPlayer
暂停。因此,当您暂停使用 any 时,请使用任何布尔变量来检查和切换它button
。
onClick(){
if(isPaused)
{
//resume media player
isPaused = false;
}else{
// pause it
isPaused = true;
}
}
回答by Noor Afshan
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 回答的那样,没有 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();
}
回答by rickdmer
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;
回答by hedisam
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.
定义一个boolean
名为playedAtLeastOnce
(或任何你想要的)的变量,然后将它设置为true
如果你的MediaPlayer
对象至少播放了一次。将它分配给 true 的一个好地方是在实现接口的onPrepared(MediaPlayer mp)
方法中MediaPlayer.OnPreparedListener
。
Then if MediaPlayer
is not playing and playedAtLeastOnce
is true
, you can say that MedaiPlayer
is paused.
那么如果MediaPlayer
不是在播放,playedAtLeastOnce
是true
,你可以说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;
}