我在Lollipop设备上使用MediaPlayer时遇到了一个问题。基本上,当设备屏幕关闭(即用户锁定设备)时,播放继续进行,但提前结束约1-2秒。如果屏幕开启,则不会出现此问题。
我在MediaPlayer上设置了onCompletionListener:
@Override
public void onCompletion(final MediaPlayer mediaPlayer) {
int progress = mediaPlayer.getCurrentPosition();
int duration = mediaPlayer.getDuration();
Log.d("PlaybackController", "progress: " + progress + " duration: " + duration);
Log.d("PlaybackController", "Delay: " + (duration - progress)); // I'm seeing a difference of 1 - 3 seconds :(.
mServiceHandler.postDelayed(new Runnable() {
@Override
public void run() {
broadcastCompleted();
}
}, Math.max(duration - progress, 0));
}
这通常会打印出:
Delay: [1500 - 3000]
。我想知道是否有我缺失的唤醒锁,但我正在使用此处提到的正确锁定:http://developer.android.com/guide/topics/media/mediaplayer.html,其中包括PARTIAL_WAKE_LOCK和WifiLock。我还漏掉了其他什么吗?