Android Mp3 play from url

21,031

Solution 1

How to play an .mp3 from the /raw folder:

Download the .mp3 file, save it to song.mp3 and paste into the /raw folder. If you don´t have /raw folder, just create it into the /res folder.

enter image description here

this example doesn´t load the .mp3 from internet, play the .mp3 from the resources.

  mediaPlayer = MediaPlayer.create(this, R.raw.song);

How to play an .mp3 from the url:,

change the oncreate() method of the example to:

  @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main_video);
      songName = (TextView)findViewById(R.id.textView4);
      startTimeField =(TextView)findViewById(R.id.textView1);
      endTimeField =(TextView)findViewById(R.id.textView2);
      seekbar = (SeekBar)findViewById(R.id.seekBar1);
      playButton = (ImageButton)findViewById(R.id.imageButton1);
      pauseButton = (ImageButton)findViewById(R.id.imageButton2);
      songName.setText("song.mp3");    
    //mediaPlayer = MediaPlayer.create(this, R.raw.song);
      Uri myUri = Uri.parse("http://searchgurbani.com/audio/sggs/1.mp3");      
      try {
          mediaPlayer = new MediaPlayer();
          mediaPlayer.setDataSource(this, myUri);
          mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
          mediaPlayer.prepare(); //don't use prepareAsync for mp3 playback
          mediaPlayer.start();
       } catch (IOException e) {           
          e.printStackTrace();
      }                  
      seekbar.setClickable(false);
      pauseButton.setEnabled(false);

   }

so you will able to play the audio mp3 from the url specified.

don´t forget to add

 <uses-permission android:name="android.permission.INTERNET"/>

into your Manifest.xml

Solution 2

Through URL

    try {
        String url = "http://www.all-birds.com/Sound/western%20bluebird.wav"; // your URL here
        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setDataSource(url);
        mediaPlayer.prepare(); // might take long! (for buffering, etc)
        mediaPlayer.start();
    }catch (Exception e){
        e.printStackTrace();
    }

Through res ---> inside --> row Folder

MediaPlayer mp2 = MediaPlayer.create(this, R.raw.genuine_);
mp2.start();
Share:
21,031
user3187131
Author by

user3187131

Updated on July 19, 2020

Comments

  • user3187131
    user3187131 almost 4 years

    How do I run the following tutorial using an online mp3 url? I tried replacing the url but it doesn't seem to be working. I want to use the same code but with the url. Does anyone have any suggestions?

    The tutorial linke: http://www.tutorialspoint.com/android/android_mediaplayer.htm The mp3 url is: http://searchgurbani.com/audio/sggs/1.mp3

  • user3187131
    user3187131 over 10 years
    I am sorry but I meant I need to play file from url not downloaded file. I already am able to do that but since I have so many files and downloading them will increase the size, I need to use a way to use url.
  • Jorgesys
    Jorgesys over 10 years
    do you have this permission? <uses-permission android:name="android.permission.INTERNET"/>
  • user3187131
    user3187131 over 10 years
    OMG!!! PERFECT!! It's finally working now. Thank you so much for the solution. :D
  • user1300788
    user1300788 about 10 years
    @Elenasys How can I play 2 songs from 2 URLs one after the other?
  • rupps
    rupps almost 10 years
    You must in fact use prepareAsync() if the mp3 comes from the internet, otherwise you'd lock your marvellous UI thread for a number of seconds. It's OK to call prepare for local files, though.
  • Roman Soviak
    Roman Soviak over 5 years
    setAudioStreamType is deprecated
  • Roman Soviak
    Roman Soviak over 5 years
    I did it in the following way: mediaPlayer.setAudioAttributes(AudioAttributes.Builder().set‌​Usage(AudioAttribute‌​s.USAGE_MEDIA) .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) .build()) mediaPlayer.setDataSource(musicURL) mediaPlayer.prepare() mediaPlayer.start()