Streaming radio stations or audio files hosted on streaming servers on Android is pretty straight-forward. But then, Android has it's limitations. It won't stream just any file or radio station. In this post, I would not be specifying the formats or protocols that Android supports. Rather, this example is just a walk through of how the MediaPlayer class should be used to stream audio files/radio stations.
For an example here, I have used a SHOUTcast radio station. The URL for the source is:
http://shoutcast2.omroep.nl:8104/
The accompanying sample project contains a ProgressBar, two Buttons (for playing and stopping the MediaPlayer).
Before running the example, one should look into the documentation of the MediaPlayer class. A look at the state diagram would perhaps help you clear to understand how it actually works.
To initialize the MediaPlayer, you need a few lines of code. There you go:
There are several other helper methods which lets you query the progress or status of the player. Jump to the docs page and you will find more information on them. You can checkout the source code here.
NOTE: This sample project is tested on Gingerbread(2.3) and should work on Froyo(2.2) and above.
For an example here, I have used a SHOUTcast radio station. The URL for the source is:
http://shoutcast2.omroep.nl:8104/
The accompanying sample project contains a ProgressBar, two Buttons (for playing and stopping the MediaPlayer).
Before running the example, one should look into the documentation of the MediaPlayer class. A look at the state diagram would perhaps help you clear to understand how it actually works.
To initialize the MediaPlayer, you need a few lines of code. There you go:
Now that the MediaPlayer object is initialized, you are ready to start streaming. Ok, not actually. You will need to issue the MediaPlayer's prepare command. There are 2 variations of this.MediaPlayer player = new MediaPlayer();player.setDataSource("http://shoutcast2.omroep.nl:8104/");
- prepare(): This is a synchronous call, which is blocked until the MediaPlayer object gets into the prepared state. This is okay if you are trying to play local files that would take the MediaPlayer longer, else your main thread will be blocked.
- prepareAsync(): This is, as the name suggests, an asynchronous call. It returns immediately. But, that obvisouly, doesn't mean that the MediaPlayer is prepared yet. You will still have to wait for it to get into the prepared state, but since this method will not block your main thread, you can use this method when you are trying to stream some content from somewhere else. You will get a callback, when the MediaPlayer is ready through onPrepared(MediaPlayer mp) method, and then, the playing can start.
player.prepareAsync();You need to attach a listener to the MediaPlayer to receive the callback when it is prepared. This is the code for that.
player.setOnPreparedListener(new OnPreparedListener(){
public void onPrepared(MediaPlayer mp) {
player.start();
}
});Once, it goes into the prepared state, you can now start playing. Simple???? Yes, of course. Just to wrap it up, to stop the MediaPlayer, you need to call the stop() method.
There are several other helper methods which lets you query the progress or status of the player. Jump to the docs page and you will find more information on them. You can checkout the source code here.
Project source for the new Eclipse + Android tool chain. Download here.
NOTE: This sample project is tested on Gingerbread(2.3) and should work on Froyo(2.2) and above.
Great tutorial.
ReplyDeleteSimple and neat. Thank you!.
Simple and Awesome tutorial!
ReplyDeletehow to retrieve the metadata from shoutcast and show?
ReplyDelete@Erik: That I haven't looked at. Sorry.
ReplyDeleteexactly what i was looking for... but one issue i ran into is when internet signal is lost, the stream stops and does not reconnect automatically. how to ensure an automatic/timed reconnect attempts?? many thanks!
ReplyDeleteis this tutorial work with android 2.1 OS ?
ReplyDeletehello i have tried the source code everything goes right but i dont receive any audio out, i tried also to chnge the url to another streaming channel but the same thing
ReplyDeleteDo you see any suspicious logs?
ReplyDeletehello excuse me how to go abt putting in the radio station website like http://shoutcast2.omroep.nl:8104/
ReplyDeletei cant seem to get a stream for mine
http://adio.listen2myradio.com/
i need a solution to this for my android project , a bit of help would help me alot quite lost now
alright i have solved my problem already realised it was a network address mistake,
ReplyDeletenow i cant seem to make my progress bar work the stream works but the progress bar doesn't show any buffer animation
@Roy: Seems to be that the source is not properly configured for streaming. Any errors you are getting in your logs?
ReplyDeletein this example,the url like this http://shoutcast2.omroep.nl:8104/ .
ReplyDeletecan i streaming with otomatic port..?? so, the port not write like that. but it automatically..
(sory if my english is bad)
please help me..
does this work on android 1.6?
ReplyDelete@ankz: I haven't tried it. But my guess is that it won't work on 1.6. Give it a try though. It might just work on a few devices.
ReplyDeleteOk I downloaded it test it emulator works fine but on the samsung glaxy any version, nothing happens. No audio whatsoever. The " buttonRecord.setEnabled(true);" after calling start never runs the button is always false. I wait for some time to see if ever starts streaming, but no audio and status never changes. Nothing strange in log cat. Could you please suggest what could be an issue since there is nothing to debug obviously.
ReplyDeleteHi, your code does not work well, recording throws unexpected error almost on all emulators and froyo 2.2 seems to be an issue particularly on Samsung i5800 with froyo 2.2. It seems on prepared listener creates an issue. Cat log mostly shows errors such as (1, -1) at the end. I did not have time to debug... would love to see a fix sometime :) Anyway remarkable work regardless, Cheers mate.
ReplyDeleteHello Kumar,
ReplyDeleteGreat thanks, for such a helpful tutorial. Can you please check why it not works for this url:
http://ib3.islambox.tv:8011/
Waiting for your response.
Regards.
Solution is here: http://stackoverflow.com/a/8833346/265167
DeleteWas this done in Eclipse? I'm a little confused on what are the files are and do you have them in a zip file to download.
ReplyDeleteThanks
Yes it was done in eclipse. But I guess you will have problems with the new adt and android tools. I will attach a zip file here that should work on the new tool chain as well.
ReplyDelete@BigMarkess: Source attached. Check the updated post.
ReplyDeleteAny idea how to make this app run as a services too? Such as if I hit the home screen it runs in the background?
ReplyDeleteSure, you can do that.
DeleteHello I have a question. How can I play this URL using this code ??
ReplyDeletehttp://109.123.116.114:7015/
Helo, how can i stream this URL (http://109.123.116.114:7015/). I tried it in app, but nothing happens :(
ReplyDeleteis nice simple concept but is missing a service and wont play on background when u exit th the app or phone goes off
ReplyDeleteHello Kumar,
ReplyDeleteyour code working fine on emulator, but when i tried to run it on my galaxy S2 (gingerbread), it didn't play. Log cat said something like below:
02-23 06:18:25.324: I/NuHTTPDataSource(2622): connect to shoutcast2.omroep.nl:8104/ @0
02-23 06:18:25.499: W/NuHTTPDataSource(2622): Server did not give us the content length!
02-23 06:18:25.564: E/HTTPStream(2622): recv failed, server is gone, total received: 4723 bytes
02-23 06:18:25.564: E/HTTPStream(2622): recv failed, errno = -1 (Operation not permitted)
02-23 06:18:25.564: E/NuCachedSource2(2622): source returned error -1004
02-23 06:18:25.704: E/MediaPlayer(9175): error (1, -2147483648)
02-23 06:18:25.704: E/MediaPlayer(9175): Error (1,-2147483648)
02-23 06:18:27.024: D/dalvikvm(7165): GC_EXPLICIT freed 5K, 49% free 2752K/5379K, external 0K/0K, paused 88ms
02-23 06:18:32.064: D/dalvikvm(7443): GC_EXPLICIT freed 7K, 47% free 3266K/6151K, external 0K/0K, paused 121ms
02-23 06:18:37.039: D/dalvikvm(2882): GC_EXPLICIT freed 309K, 50% free 3224K/6343K, external 0K/0K, paused 96ms
02-23 06:18:59.999: I/AlarmManager(2711): wakelock acquire, uid:1000 at elapsed real time: 35625310
02-23 06:19:00.054: I/AlarmManager(2711): wakelock release, uid:1000 at elapsed real time: 35625367
02-23 06:19:49.729: I/AlarmManager(2711): wakelock acquire, uid:10013 at elapsed real time: 35675045
02-23 06:19:49.749: I/AlarmManager(2711): wakelock release, uid:10013 at elapsed real time: 35675065
please advice. thanks