Shay
Shay

Reputation: 53

How to listen to android MediaPlayer position change event

While playing a video nor audio via the Android MediaPlayer how can one receive every 10 ms (or any specific delay) the current player position, while its playing (and stop receiving while its not playing)

I prefer not to use handler that post delay itself. It needs to be cancelable.

Upvotes: 3

Views: 2652

Answers (1)

Hasan Bou Taam
Hasan Bou Taam

Reputation: 4035

You can use a Timer:

//member field 
Timer timer;


//somewhere in your code:

timer = new Timer();

timer.schedule(new TimerTask() {

@Override
public void run() {

if(mediaPlayer.isPlaying()){

//get current position of playback because player is playing

int position = mediaPlayer.getCurrentPosition();
Log.d("POSITION" , String.valueOf(position));

}else{

//player is not playing do nothing.......

}


}

},0,1000); //1000 is a long value in milliseconds (in this case it runs every 1 second)

When you want to cancel the timer:

timer.cancel();

Upvotes: 3

Related Questions