how to ShoutCast Streamming with MediaPlayer with android 2.3.3

Rako picture Rako · Jul 16, 2012 · Viewed 9.2k times · Source

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 !!

Answer

Rako picture Rako · Jul 17, 2012

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
}