Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Audio streaming with Android MediaPlayer

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.
like image 772
Fernando Valente Avatar asked Apr 26 '11 02:04

Fernando Valente


People also ask

What class in Android can play audio?

One of the most important components of the media framework is the MediaPlayer class. An object of this class can fetch, decode, and play both audio and video with minimal setup.

Where is the media player on Android 13?

A redesigned media player One of the more obvious upgrades you'll see on Android 13, is a redesigned media player widget. It shows up under Quick Settings and on the lock screen when media is playing.


2 Answers

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.

like image 164
Geobits Avatar answered Sep 23 '22 09:09

Geobits


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.

  • the project http://code.google.com/p/aacplayer-android/ is licensed under GPL, so you can create commercial apps * on top of it, but you need to fullfill the GPL - mainly it means to publish your code as well. * If you use the second project http://code.google.com/p/aacdecoder-android/ , then you do not need to publish your * code (the library is licensed under LGPL).
like image 34
Yaqub Ahmad Avatar answered Sep 26 '22 09:09

Yaqub Ahmad