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

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.

Thanks..

###

simple method to do this ::

MediaPlayer mp = new MediaPlayer();
 mp.setDataSource(PATH_TO_FILE);
 mp.prepare();
 mp.start();

###

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() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            killMediaPlayer();
        }
    });
    mediaPlayer.start();
}

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

In your Activity,example,just call
PlayAudioManager.playAudio(context, http://dict.youdao.com/dictvoice?audio=good&type=1)

###

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.prepareAsync();

                mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

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

                mp.start();

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

                songProgressBar.setProgress(0);
                songProgressBar.setMax(100);

                // Updating progress bar
                updateProgressBar();

            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        else
        {
            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..

###

Edit:

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:

http://www.w3schools.com/html/html_sounds.asp

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" />
</audio>

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 *