Example of streaming mp3 mediafile from URL with Android MediaPlayer class

Few days ago i tried to make my Android app can play music from URL.

The Android API give to us a very useful class. Here i want show you how to make  the streaming m3 mediaplayer using MediaPlayer class from Android API. This app use Android 2.2.

Below you can see the UI of this app. It’s very simple. Also below you can get the link for downloading source code

Android Streaming mp3 player's screenshot

Here i show you only Activity file (StreamingMp3Player.java).

I hope it will be useful for you.

Please download if you want to see some other file from this Android project.

Download it from github

80 thoughts on “Example of streaming mp3 mediafile from URL with Android MediaPlayer class

  • By mian - Reply

    Hi, Igor.

    Thanks a lot, nice and useful tutorial. Good job.

    May i ask you how can we set the autoplay please? I’m trying to figure out how to start streaming automatically when you open the program but… I have not yet understood how to do … hehe
    Thanks again.

  • By Mountanous Maverick - Reply

    nice site the text becomes a bit hard to read on your background after a while tho. jus a friendly suggestion =)

  • By fahmi - Reply

    Thanks, brother

  • By Igor - Reply

    Hello Mountanous Maverick,

  • By Linus - Reply


    Very new to Android development. Cannot figure out problem in downloaded sample code. Eclipse says the import com.hrupin.media.R cannot be resolved. Also many mentions of R cannot be resolved to a variable. Sounds like basic problem but I don’t know what I’m missing.


  • By Igor - Reply

    Hi Linus,

    1. Check your Android SDK connection in Eclipse prefferences.
    2. Clean your project
    3. If it don’t help – delete bin dir manullay

  • By Thirumal - Reply

    Thank You Very much

  • By mario - Reply

    good job man

  • By Lavanya - Reply

    Hi, I am getting a delay of some seconds before the audio is started. Can you please let me know the issue. I have tried the same code. After I call prepareAsync(), its calling start() after about 24seconds. Thank you.

  • By rahulbawa - Reply

    I am just thinking that if the server will get the request and keep writing music bytes on the client’s output stream and may be the setDataSource method process what’s being written onto it and plays it.
    Am I thinking in the right way?
    So if above is correct then I’ll have to write some server code too?

  • By Crashouille - Reply

    Hi !

    First of all, thanks for this great article. This was exactly what I was looking for. However, it seems that the source code in your GIT repository has since changed to become a video player. Is there a way, by any chance, to get the original code fitting this article ?

    Thanks ahead for your answer.

  • By Tolbxela - Reply

    I have recreated the sample of Android Streaming Mp3 Player.
    The code can be downloaded here: https://github.com/tolbxela/samples/tree/Android-Streaming-Mp3-Player/android/Streaming_Audio_Player

    • By Igor Khrupin - Reply

      Merged. Thanks for your work

      • By Tolbxela - Reply

        You’re welcome!

  • By Christian - Reply


    Thanks for the this. I still got an issue with a device running on 2.3.6. It gives me error (1, -4) while another device with 4.2 works fine.

    • By Igor Khrupin - Reply

      Hi Christian,

      Could you please send logcat from your device? When I get it then I can look what is happened.


  • By Kirti Avaiya - Reply

    Its very good example, saves my days to search and research , very great tutorial

  • By abdul - Reply

    how to download this project?

  • By Srikanth - Reply


    Thanks for very good tutorial. Plz Can u tell me ” How to show progress dialog ” before first download.


  • By atefeh.soveyzi - Reply

    tanks…tanks a lot

    • By Igor Khrupin - Reply

      You’re welcome!

  • By Marco - Reply

    Very nice tutorial! Can I run this app in background? So I can listen the mp3 even with screen off.

    • By Igor Khrupin - Reply

      Hi Marco,

      Yes, you can. You need create Service and implement mediaplayer inside it. I try make post with it.

      • By Marco - Reply

        Wow, thank you very much!!!!

    • By Tolbxela - Reply

      Hello Marco,

      take a look at my project https://github.com/tolbxela/AerostatApp.
      I took this great tutorial as a basis and built a complete app with a background player.
      If you look at the first versions, you will see how I step be step changed the original code.
      Feel free to ask!

      Good Luck!

      Many thanks to Igor Khrupin!

      • By Marco - Reply

        Thank you very much, I really appreciate your help!!!

  • By Mohammad Afnan - Reply

    Thanks a lot, this tutorial is very helpful to me.

Leave a Reply

Your email address will not be published. Required fields are marked *

− four = 1

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">