Streaming Radio Stations on Android

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:

MediaPlayer player = new MediaPlayer();

player.setDataSource(“http://shoutcast2.omroep.nl:8104/”);

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.

  1. 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.
  2. 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.

So, for our example, the best choice would be:

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.

60 thoughts on “Streaming Radio Stations on Android

  1. droid noob

    exactly 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!

  2. Roy

    alright i have solved my problem already realised it was a network address mistake,

    now i cant seem to make my progress bar work the stream works but the progress bar doesn't show any buffer animation

  3. Anonymous

    Ok 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.

  4. Anonymous

    Hi, 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.

  5. Anonymous

    Hello Kumar,

    your 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

  6. Heygel

    Hey, thanks for your very useful tutorial, it's a great help for beginners in android development like me. I tried your code, and it worked. BTW I'm making online radio app and I want the title of the playing song to be viewed. I have no idea about this, could you give me some hints? Thanks in advance.

  7. Pr@nav

    Hey i developed simple radio app using your guideline…but my application have service that actually allow me to run the radio as background process..but many times service not working properly for Stop and Start service for radio…so please tell me any solution to handle the service so that it work properly..

    Thanks,
    Pranav

    1. Pr@nav

      while working with app..i need to stop and start the service for radio app and to run the next station…but many times the service remain start…and when i came back to application from outside to radio app…it suddenly stop whatever station running in….

  8. Pr@nav

    while stopping and starting the service it not working properly..means it just stop itself when i came back to application from outside while radio is running as service…

    1. Pr@nav

      ohhh…that's ok…i checked out already but i think its service problem….that i cant manage but as suggested in android doc i prepared service for starting and stopping it..but sorry..working..
      If u have solution the please suggest me or if u have any one who expert in it or actually have developed such application nice to welcome u….please

  9. nishant

    Thanks for the code. Now I am able to play the station using southcast stream but I also want to get Metadata of the stream like title, station name. Can you please share the code to achieve this in Android?

  10. Michael Foster

    Kumar, it seems lime many people are complaining about not hearing sound on some devices. i wrote a custom streamer myself and have had the same problems. Have you been able to figure out why this happens? It's been driving my crazy and i have no clues. thanks!!

  11. Michael Foster

    Kumar, it seems that many people have complained about not hearing sound on some devices. i have written my own streaming code and have the same problems. Any ideas why this happens? I've been trying to figure it out and have no clue. Thanks for the great article!

Leave a Reply