MediaPlayer isPlaying()总是返回false

时间:2011-11-29 06:47:09

标签: java android media-player

我正在测试2部手机。在一个设备中,即使正在播放,媒体播放器的播放()也始终返回false。但是在其他设备中(lg optimus gingerbread)isPlaying()返回true是否正在播放else返回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

感谢。

1 个答案:

答案 0 :(得分:1)

此外,虽然我们使用的是一般的MediaPlayer主题,但请注意,在UI线程上调用isPlaying()会导致ANR。事实上,Google建议从后台处理程序线程执行所有MediaPlayer调用。这也将序列化MediaPlayer调用的执行。