【发布时间】:2011-11-29 06:47:08
【问题描述】:
我正在 2 部手机上进行测试。在一个设备中,媒体播放器的 isPlaying() 总是返回 false,即使它正在播放。但在其他设备(lg Optimus 姜饼)中,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
谢谢。
【问题讨论】:
-
你检查过try catch空子句是否抛出异常吗?
-
不,我在每次捕获中都在跟踪日志。所以还没有抛出异常。
-
我刚刚从设备上卸载并再次从 Eclipse 重新运行/安装项目。现在它的工作。奇怪。
-
现在对你来说可能不值得..但是在阅读 isPlaying() 之前检查一次是否调用 MediaPlayer.pause()/stop()/release()/reset(),我做到了同样的错误。
标签: java android media-player