pad*_*apa 3 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);
}
Run Code Online (Sandbox Code Playgroud)
现在我在打电话
mPlayer.getPlayer(currentPlayingPhraseIndex).isPlaying();
//which is now returning false. Previously i was returning true but now it always return false
Run Code Online (Sandbox Code Playgroud)
我也匹配了对象,这是现在的代码场景:
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);
Run Code Online (Sandbox Code Playgroud)
我还验证了两者是否是同一媒体
mPlayer.getPlayer(currentPlayingPhraseIndex).equals(mPlayer.getPlayer(phraseIndexToBePlayed)); //it returns true so they are both same media
Run Code Online (Sandbox Code Playgroud)
谢谢.
MediaPlayer非常奇怪且非常脆弱.你几乎无法判断它处于什么状态 - 而你所知道的任何事情都可能最终出错.
作为旁注,我发现Eclipse有时会让应用程序做得非常奇怪,莫名其妙.清理项目并重新启动Eclipse并卸载并重新安装应用程序通常会处理那些非常糟糕的事情,因此当您发现似乎不可能的错误时,请务必尝试.
使用MediaPlayer时,请自行跟踪它应该做什么,然后永远不要相信MediaPlayer将处于您离开的状态.无论何时你对MediaPlayer做任何事情,都要为那可爱的IllegalStateException做好准备 - 也许它正在播放,也许它已经准备好了,也许它已经暂停了,但是你应该做好准备,在任何时候,它都可能处于错误的状态.当您以这种方式编写代码时,您不太依赖于isPlaying()的准确性.
我知道没有用,但不幸的是,我们没有太多替代MediaPlayer的方法.
| 归档时间: |
|
| 查看次数: |
4381 次 |
| 最近记录: |