MediaPlayer isPlaying() 总是返回 false

2023-12-07

我正在 2 部手机上进行测试。在一台设备中,媒体播放器的 isPlaying() 始终返回 false,即使它正在播放。但在其他设备(lg optimusgingerbread)中,如果正在播放,则 isPlaying() 返回 true,否则返回 false。

谁能告诉我为什么会发生这种情况?我的一些代码取决于 isPlaying() 部分,我想让它在所有设备上工作。

public void addSoundMedia(Context context, int resId){
    MediaPlayer mp = MediaPlayer.create(context, resId);
    mp.setVolume(1.0f, 1.0f);
    try{
    mPlayerList.add(mp);
    }
    catch(Exception e){}
}

public void playSoundMedia(int index){
    try{        
    mPlayerList.get(index).start();         
    }
        catch(Exception e){}
}


public MediaPlayer getPlayer(int index){
    return mPlayerList.get(index);
}

现在我打电话

mPlayer.getPlayer(currentPlayingPhraseIndex).isPlaying(); 
//which is now returning false. Previously i was returning true but now it always return false

而且我也匹配了这个对象, 这是现在的代码场景:

int phraseIndexToBePlayed = getRandomInteger(0, numberOfPhrasesSelected-1, randomPhraseGen); //get random index from 0 to (size of arraylist where i added my media) -1
currentPlayingPhraseIndex = phraseIndexToBePlayed; //for later use in another function scope

mPlayer.playSoundMedia(phraseIndexToBePlayed);

我还验证了两者是否是相同的媒体

mPlayer.getPlayer(currentPlayingPhraseIndex).equals(mPlayer.getPlayer(phraseIndexToBePlayed)); //it returns true so they are both same media

Thanks.


MediaPlayer 非常奇怪而且非常脆弱。你几乎无能为力来判断它处于什么状态——而且你所知道的任何事情最终都可能出错。

附带说明一下,我发现有时 Eclipse 可以让应用程序执行非常奇怪、无法解释的事情。清理项目并重新启动 Eclipse 以及卸载并重新安装应用程序通常可以解决那些非常奇怪的事情,因此当您发现似乎不可能的错误时,请务必尝试这样做。

使用 MediaPlayer 时,请自己跟踪它应该执行的操作,然后永远不要相信 MediaPlayer 会处于您离开时的相同状态。每当您对 MediaPlayer 执行任何操作时,请为可爱的 IllegalStateException 做好准备 - 也许它正在播放,也许它已准备好,也许它已暂停,但您应该做好准备,在任何时候它都可能处于错误的状态。当您以这种方式编写代码时,您就不太依赖 isPlaying() 的准确性。

我知道没有帮助,但不幸的是我们没有太多 MediaPlayer 的替代品。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MediaPlayer isPlaying() 总是返回 false 的相关文章

随机推荐