6

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.
sealz
  • 5,348
  • 5
  • 40
  • 70
  • Are you running this in the emulator? I've had problems running media in the emulator before. Try it in an actual device, maybe it will work. – dmon Apr 26 '11 at 02:38
  • Yes, I'm running it on the emulator. Too bad I don't have a real device :( – Fernando Valente Apr 26 '11 at 02:40
  • My hunch is that that's the problem. Someone else might have another take. – dmon Apr 26 '11 at 02:50
  • 1
    Just for kicks, set your data source to `http://87.230.103.107:8000/;`. If it works, it means your user-agent is Mozilla, which causes the SHOUTcast server to send back the admin interface rather than the stream. The semicolon forces it to return the stream. – Brad Apr 26 '11 at 13:03

3 Answers3

5

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.

Geobits
  • 22,218
  • 6
  • 59
  • 103
3

The problem is that content type "audio/aacp" streaming is not supported directly . Some decoding library can be sued to play "aacp", please see the solution below:

Freeware Advanced Audio (AAC) Decoder for Android

How to use this library?

For more detail please see this.

Consider legal issues while using it.

Community
  • 1
  • 1
Yaqub Ahmad
  • 27,569
  • 23
  • 102
  • 149
-1

1)Avoid using prepare(), use prepareAsyc() instead. (or) put your playing logic in a worked thread or a separate thread. 1)Avoid using player.prepare(), instead use player.prepareAsync();

(or) keep the logic in a separate thread. (you can use intent service, AsyncTask,etc)

2)Also try with static constructor MediaPlayer.create(Uri);

Sree Rama
  • 1,207
  • 12
  • 25