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 !
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 !
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;
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;
}
}
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;
}
You can create global variable
private boolean notIdle = false;
After you setDataSource()
, in the listener setOnPreparedListener()
notIdle = true;
Override MediaPlayer.reset()
funtion, inside it
notIdle = false;
notIdle
will be true
in start()
, pause()
and stop()
states... you can change this behaviour as you consider more convinient.
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();
}
© 2022 - 2025 — McMap. All rights reserved.