Audio streaming with Android MediaPlayer

Fernando Valente picture Fernando Valente · Apr 26, 2011 · Viewed 8.7k times · Source

I'm trying to create an audio streamer with Android's MediaPlayer. It's just fine if it doesn't work with Android 2.1 or bellow. I need to be able to play the audio from a SHOUTcast stream. Here's my code:

player = new MediaPlayer();
try {
player.setDataSource("http://87.230.103.107:8000");
player.prepareAsync();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
    e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
player.start();

For some reason, this code will play just nothing. I think it may be related to the app permissions. Does anyone know what's going on?

Thanks!

[UPDATE] I'm getting the following errors:

04-25 23:35:15.432: ERROR/MediaPlayer(283): start called in state 4
04-25 23:35:15.432: ERROR/MediaPlayer(283): error (-38, 0)
04-25 23:35:15.602: ERROR/MediaPlayer(283): Error (-38,0)
04-25 23:35:17.542: INFO/AwesomePlayer(33): calling prefetcher->prepare()
04-25 23:35:18.547: INFO/Prefetcher(33): [0x17650] cache below low water mark, filling cache.
04-25 23:35:18.762: INFO/AwesomePlayer(33): prefetcher is done preparing
04-25 23:35:19.769: ERROR/AwesomePlayer(33): Not sending buffering status because duration is unknown.

Answer

Geobits picture Geobits · Jun 22, 2011

Sorry for the late response, ran across this while looking for answers to another problem.

If you still need an answer, you're calling start() while it's still being prepared. PrepareAsync() returns immediately, unlike prepare(). The problem with using 'prepare()` with streams is that it will block until it has enough data to start play.

What you want to do is set an OnPreparedListener, and have the start() called from there.