MediaPlayer的IsPlaying模块()总是返回false模块、MediaPlayer、IsPlaying、false

2023-09-07 00:10:04 作者:抛弃一切世俗

我测试的2个手机。在一台设备IsPlaying模块媒体播放器的()总是返回false甚至当它被播放。但在其他设备(LG擎天柱姜饼)是是玩否则返回假IsPlaying模块()返回true。

谁能告诉我为什么发生这种情况?我的一些$ C $,c取决于这个IsPlaying模块()的一部分,我希望把它在所有设备上工作。

 公共无效addSoundMedia(上下文的背景下,INT渣油){
    MediaPlayer的MP = MediaPlayer.create(背景下,渣油);
    mp.setVolume(1.0F,1.0F);
    尝试{
    mPlayerList.add(MP);
    }
    赶上(例外五){}
}

公共无效playSoundMedia(INT指数){
    尝试{
    mPlayerList.get(指数)。开始();
    }
        赶上(例外五){}
}


公共MediaPlayer的getPlayer(INT指数){
    返回mPlayerList.get(指数);
}
 

现在我打电话

  mPlayer.getPlayer(currentPlayingPhraseIndex).isPlaying();
//这是现在返回false。 previously我是返回true,但现在它总是返回false
 

我也匹配的对象, 这是code情景现在:

  INT phraseIndexToBePlayed = getRandomInteger(0,numberOfPhrasesSelected-1,randomPhraseGen); //从0随机指数(数组列表在这里我补充我的媒体的尺寸)-1
currentPlayingPhraseIndex = phraseIndexToBePlayed; //在另一个函数范围以后使用

mPlayer.playSoundMedia(phraseIndexToBePlayed);
 
Windows Media Player 12新功能介绍

我也验证了如果两个相同的媒体

mPlayer.getPlayer(currentPlayingPhraseIndex).equals(mPlayer.getPlayer(phraseIndexToBePlayed)); //它,它们都是相同的介质返回true

感谢。

解决方案

MediaPlayer的是非常奇怪的,非常脆弱。你很少可以做,告诉它处于什么状态 - 和任何你知道可能最终错

作为一个方面说明,我发现,有时Eclipse可以让应用程序做的很奇怪,莫名其妙的事情。清洁项目并重新启动Eclipse和卸载并重新安装应用程序通常需要的那些真的靠不住的后事,所以最好是当你发现一个错误,似乎是不可能的。

在与MediaPlayer的工作,跟踪自己什么应该做的事情,然后永远不会相信的MediaPlayer将在您离开时的相同状态。每当你做任何事情与MediaPlayer的,是ppared一个愉快IllegalStateException异常$ P $ - 也许这是玩,也许是prepared,也许它已暂停,但你应该是prepared,在任何点中,可能在错误的状态。当你写你的code这样一来,你是在IsPlaying模块()是正确的。

的依赖

没有帮助我知道,但遗憾的是我们没有太多的替代品的MediaPlayer的方法。

I am testing on 2 phones. In one device isPlaying() of mediaplayer always return false even when it is playing. but in other device (lg optimus gingerbread) isPlaying() returns true is it is playing else returns false.

Can anyone tell me why this is happening ? My some code depends on this isPlaying() part and i want to make it work on all devices.

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);
}

Now i am calling

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

And i also matched the object, this is the code scenario now:

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);

i also verified if both are same media

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

Thanks.

解决方案

MediaPlayer is very odd and very fragile. You have very little you can do to tell what state it is in--and anything you do know could end up wrong.

As a side note, I have discovered that sometimes Eclipse can make apps do VERY strange, inexplicable things. Cleaning the project and restarting Eclipse and uninstalling and reinstalling the app usually takes care of those really wonky things, so always try that when you find an error that seems impossible.

When working with MediaPlayer, track for yourself what it should be doing, and then never, ever trust that MediaPlayer will be in the same state where you left it. Anytime you do anything with the MediaPlayer, be prepared for that lovely IllegalStateException--maybe it was playing, maybe it was prepared, maybe it was paused, but you should be prepared that, at any point, it may be in the wrong state. When you write your code this way, you are less reliant on isPlaying() being accurate.

Not helpful I know, but unfortunately we don't have much in the way of alternatives to MediaPlayer.