Android MediaPlayer can not play the audio file online but work well if load the same one from local

时间:2017-04-10 02:50:47

标签: android audio media-player android-mediaplayer

Some thing weird happened when I use android MediaPlayer to play some audio stream from service.Here is my audio stream url: http://7xk2r8.com1.z0.glb.clouddn.com/rNeDSn1Octc When I use it with MediaPlayer like this:

mediaPlayer.setDataSource("http://7xk2r8.com1.z0.glb.clouddn.com/rNeDSn1Octc")

The onCompleteListener will be called immediately

But if I download the file from above url to android device,and use MediaPlayer to load it from local like this:

mediaPlayer.setDataSource("local path")

Everything works fine!

I think the bugs may from the stream file itself,but I don't know why,I have been lost in it for about 2 weeks,Help!!!!!!

PS:I used the most simple code for MediaPlayer,So I think it's fine with my code,I am sure that the bugs from audio stream file itself,could you test the file?The file URL above is totally public.Thanks!

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。 Mediaplayer有时无法加载网络代理等原因。您可以调用prepareAsync()但它永远不会调用onPrepared()。所以我添加了一个布尔标志,如下所示:

@Override
public void onPrepared(MediaPlayer mp) {
    isPrepared = true;}

@Override
public void onCompletion(MediaPlayer mp) {
    if(isPrepared) {// Do your code}
相关问题