Java ISPlay()上Android MediaPlayer状态不匹配错误的解决方法?

Java ISPlay()上Android MediaPlayer状态不匹配错误的解决方法?,java,android,Java,Android,我试图找出如何解决Android MediaPlayer的“状态不匹配”错误,当我尝试暂停时,在音频播放过程中偶尔会抛出该错误 如中所述,Android MediaPlayer在调用isplay() 结果是调用pause()或isplay()会导致MediaPlayer停止响应请求,直到重置为止 以下是发生此错误时的日志: I/MusicPlaybackService﹕ I got a pause message E/MediaPlayer[Native]﹕ internal/external

我试图找出如何解决Android MediaPlayer的“状态不匹配”错误,当我尝试暂停时,在音频播放过程中偶尔会抛出该错误

如中所述,Android MediaPlayer在调用
isplay()

结果是调用
pause()
isplay()
会导致
MediaPlayer
停止响应请求,直到重置为止

以下是发生此错误时的日志:

I/MusicPlaybackService﹕ I got a pause message
E/MediaPlayer[Native]﹕ internal/external state mismatch corrected

我目前的解决方案非常丑陋:

/**
 * 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重新设置其自身状态的概念

此解决方案是黑客攻击的,似乎正在导致

谷歌似乎已经将这种行为的原因标记为过时

我正在运行安卓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

注释供参考:


该问题被脚本标记为过时,因为原始报告在Android 2.3之前。对此的评论建议打开一个新的。您是否在实例化对象后立即尝试调用mp.reset()?它解决了一些人的问题,但没有造成更多的问题。我自己没试过,所以是的。@ManPerson我会试一下。我犹豫是否要重置,因为我不想在暂停发生时重新启动歌曲。我想你只需要在实例化MediaPlayer对象时进行重置,就这样