how to ShoutCast Streamming with MediaPlayer with android 2.3.3

若如初见. 提交于 2019-12-22 17:46:17

问题


Hi all and thanks in advance,

I have been all day looking forums and on internet and i'm not getting any clear about this. I am not sure if it possible in a direct and simple way. I read all time that since 2.2 it is supported natively but i don't see and example where it works easily

First, i make some test with some .mp3 on a web and this code worked fine:

mp = new MediaPlayer();
mp.setDataSource(localContext, Uri.parse(SomeURL.mp3));     
mp.prepare();
mp.start()  

but now....i have to reproduce Stream audio, and i have been given just an ip and port from a shoutcast server, i am trying all kind of things but all time i get error in media player, the typical (0,-38) error, general.

mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
//mp.setDataSource("http://xxx.xxx.xxx.xxx:xxxx");          
mp.setDataSource("http://xxx.xxx.xxx.xxx");
mp.prepareAsync();
mp.start()  --> it crashs here  

i have no idea if i am doing it correct....first time i face stream issues....

Is it possible to make it a easy way like that? if not....what are the solutions?

Thanks !!


回答1:


After two days of googling without much result, I found a page with a lot of public streaming url link and I tried some with the original code and in windows media player. Almost all of then didn't work on wmp but some yes...so I tried some of them, and the ones with link it didn't worked...but the ones with ip+port yes !! It seems the problem was with the ip+port i had for tests.... So my code is really simple finally.... and it works for a spanish radio.

if(!mp.isPlaying()){
  try{
    mp = new MediaPlayer();
    mp.setOnPreparedListener(this);
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
    url="http://91.121.92.186:8060";
    mp.setDataSource(url);
    mp.prepareAsync();
  }catch(IOException e){
    e.printStrackTrace();
  }
}

@Override
public void onPrepared(MediaPlayer mp) {
    mp.start();//Cuando acaba de cargar inicia la reproducción
}



回答2:


The code you provided crashes because if u use "mp.prepareAsync();" then u cant "mp.start" you have to use on prepared listener as "mp.setOnPreparedListener(new OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer arg0) {
                    // TODO Auto-generated method stub
                    mp.start();
                    }});"



回答3:


Why don't you take a look at:

Online radio streaming app for Android

or

http://code.google.com/p/rxwen-blog-stuff/source/browse/trunk/android/streaming_audio/

or maybe

http://erkutaras.blogspot.fr/2011/08/stream-audio-from-url-example-android.html

And tell us if It worked

;-)



来源:https://stackoverflow.com/questions/11504809/how-to-shoutcast-streamming-with-mediaplayer-with-android-2-3-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!