【问题标题】:Approaches to Android MediaPlayer state mismatch error on isPlaying()?isPlaying() 上的 Android MediaPlayer 状态不匹配错误的方法?
【发布时间】: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 对象时这样做就可以了

标签: java android


【解决方案1】:

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

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供参考:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多