【发布时间】:2015-09-01 10:28:05
【问题描述】:
我正在尝试找出如何解决 Android MediaPlayer 的“状态不匹配”错误,该错误在我尝试暂停时偶尔会在音频播放期间引发。
如this question 中所述,Android MediaPlayer 存在一个已知问题,在调用isPlaying() 时偶尔会抛出错误
结果是调用pause() 或isPlaying() 导致MediaPlayer 停止响应请求,直到它被重置。
这是发生此错误时的日志:
I/MusicPlaybackService﹕ I got a pause message
E/MediaPlayer[Native]﹕ internal/external state mismatch corrected
Here's a github bug with more details related to this issue。
我目前的解决方案是非常难看:
/**
* Pause the currently playing song.
*/
private synchronized void pause() {
try{
// this is a hack, but it seems to be the most consistent way to address the problem
// this forces the media player to check its current state before trying to pause.
int position = mp.getCurrentPosition();
mp.seekTo(position);
mp.start();
mp.pause();
} catch (Exception e){
Log.w(TAG, "Caught exception while trying to pause ", e);
}
updateNotification();
}
我的理论是 MediaPlayer 失去了对自己状态的跟踪,在暂停之前调用 start() 和 seekTo() 将强制 MediaPlayer 重置其自身状态的概念。
这个解决方案很老套,似乎导致other issues。
Google 似乎已将此行为的 open issue 标记为已过时。
我正在运行 android 5.0.1 的 LG G3 上进行测试。
因此我的问题是:我应该怎么做?有没有更好的方法来强制 MediaPlayer 在暂停前检查自己的状态?
【问题讨论】:
-
该问题被脚本标记为已过时,因为原始报告在 Android 2.3 之前。对此的评论建议打开一个新的。
-
您是否尝试过在实例化对象后立即调用 mp.reset()?它解决了某些人的问题而不会造成更多问题。我自己没试过,所以是的。
-
@ManPerson 我会试试这个。我犹豫要不要重置,因为我不想在暂停时重新开始歌曲。
-
我认为你只需要在实例化 MediaPlayer 对象时这样做就可以了