当通过Android MediaPlayer播放视频或音频时,如何才能在播放时每10毫秒(或任何特定的延迟)接收当前播放器的位置(并在不播放时停止接收)。
我不喜欢使用延迟处理程序,它需要是可取消的。
你可以使用 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)
当您想取消定时器时。
timer.cancel();