Android : how to know if MediaPlayer is paused?
Asked Answered
I

5

18

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 !

Instrument answered 4/3, 2014 at 9:25 Comment(1)
see thisReplace
K
20

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;
Kristankriste answered 4/10, 2015 at 21:53 Comment(1)
Docs say that getCurrentPosition return "the current position in milliseconds". If you've never put the mediaPlayer in pause, it will be a huge number like 1628054332 or similar (probably system time in millis since 1970). So to take care of the first time the user plays (without previously using pause) you will have to introduce a flag of some sorts.Perky
C
5

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;
  }
} 
Conto answered 4/3, 2014 at 9:30 Comment(1)
If there is no such method, then How you are going to set value of isPaused variable ?Burnight
N
1

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;
}
Nadeen answered 10/7, 2016 at 20:8 Comment(0)
M
0

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.

Mousse answered 16/1, 2023 at 4:11 Comment(0)
D
-1

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();
}
Dumbwaiter answered 24/4, 2014 at 8:40 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.