简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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