android – How can I play a mp3 without download from the url?-ThrowExceptions

Exception or error:

I want to develop as follows but I did not find good example:

I want to play without having to download an mp3 encoding in the url.
Unless the user stops it the mp3 should play in the background.
I guess I do multitasking.

Is there an example document or any advice please.

How to solve:

Look at these two tutorials, In these the .mp3 files are playing through web url,

Example of streaming mp3 mediafile from URL with Android MediaPlayer class

Play Mp3 file from a Url

Also if you want to play .mp3 file in background I think you have to use Service and AIDL for it,

Look at basic Android-Music Player demo MusicDroid – Audio Player Part II it describe how to use Service and AIDl for your Audio Player.



simple method to do this ::

MediaPlayer mp = new MediaPlayer();


public class PlayAudioManager {
    private static MediaPlayer mediaPlayer;

public static void playAudio(final Context context, final String url) throws Exception {
    if (mediaPlayer == null) {
        mediaPlayer = MediaPlayer.create(context, Uri.parse(url));
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        public void onCompletion(MediaPlayer mp) {

private static void killMediaPlayer() {
    if (mediaPlayer != null) {
        try {
            mediaPlayer = null;
        } catch (Exception e) {

In your Activity,example,just call


You can use
MediaPlayer.create(context, uri);
pass the context(this) and the uri from where you want to play the song


if (!isPlaying)
            isPlaying = true;

            mp = new MediaPlayer();

            try {

                mp.reset(); // new one

                mp.setDataSource(AudioPlayer.this, myUri);



                mp.prepare(); // don't use prepareAsync for mp3 playback


                // String songTitle = songsList.get(songIndex).get("songTitle");
                   // songTitleLabel.setText(songTitle);


                // Updating progress bar

            } catch (IllegalArgumentException e) {
            } catch (IllegalStateException e) {
            } catch (IOException e) {

            isPlaying = false; 

            mp.release();// stop Playing 

            //mp = null;

I have use it in my MediaPlayer it is working .. there is no need to download it..



Oh, this is Android specific, not (just) a web site. I’d suggest you look both at these solutions, and one of the MediaPlayer solutions other people mentioned. If you go with MediaPlayer, I recommend you accept their answer 🙂

There are many examples of how to do this on w3schools:

You can:

  • Embed a media player, such as the Yahoo web player
  • Play audio in HTML via the <audio> element
  • Use the <embed> element
  • Use the <object> element

Here’s the solution they describe as “best”:

<audio controls="controls" height="50px" width="100px">
  <source src="song.mp3" type="audio/mpeg" />
  <source src="song.ogg" type="audio/ogg" />
  <embed height="50px" width="100px" src="song.mp3" />

Note that this is an HTML 5 element, so might not work on every single browser out there. I think it will probably work in any browser you should be developing rich multimedia experiences in, though.

Leave a Reply

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