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/M开发者_如何学编程ediaPlayer(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.
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.
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).
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);
精彩评论