gpt4 book ai didi

java - 解决 isPlaying() 上的 Android MediaPlayer 状态不匹配错误的方法?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:46 26 4
gpt4 key购买 nike

我正在尝试解决 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 重置其概念自己的状态。

这个解决方案是 hacky,似乎导致 other issues .

Google 似乎标记了 open issue此行为已过时。

我正在运行 android 5.0.1 的 LG G3 上对此进行测试。

因此我的问题是:我应该怎么办?有没有更好的方法强制 MediaPlayer 在暂停前检查自己的状态?

最佳答案

这是我能想出的最佳解决方案。它似乎解决了暂停问题,并且没有造成任何意外的副作用:

private synchronized void pause() {
// Sometimes the call to isPlaying can throw an error "internal/external state mismatch corrected"
// When this happens, I think the player moves itself to "paused" even though it's still playing.
try{
// this is a hack, but it seems to be the most consistent way to address the problem
int position = mp.getCurrentPosition();
mp.stop();
mp.prepare();
mp.seekTo(position);
} catch (Exception e){
Log.w(TAG, "Caught exception while trying to pause ", e);
}
}

此解决方案强制重置状态机,而不重置整个媒体播放器,也不调用 play

注释 state machine供引用:

enter image description here

关于java - 解决 isPlaying() 上的 Android MediaPlayer 状态不匹配错误的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32168816/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com