5

I'm currently have a media player that is streaming an mp3 file. When that file is done, what is the code so that it goes to the next url/mp3?

And also, is there code to get the name of the file and display it? how would I go about doing that?

thanks

EDIT

see my code below:

package com.example.m3uplayer;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.media.MediaPlayer.OnCompletionListener;

public class m3uPlayer extends Activity implements MediaPlayer.OnCompletionListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //

        //http://dl.dropbox.com/u/24535120/AfroJack-UMF11-clip2.mp3

        //http://dl.dropbox.com/u/24535120/Avicii%20clip%201.mp3


        Uri myUri = Uri.parse("http://dl.dropbox.com/u/24535120/AfroJack-UMF11-clip2.mp3");

        MediaPlayer sdrPlayer = new MediaPlayer();

        try {
            sdrPlayer.setDataSource(this, myUri);//"http://mp1.somafm.com:8032");
            sdrPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            sdrPlayer.prepare(); //don't use prepareAsync for mp3 playback
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        sdrPlayer.start();
    }

    @Override
    public void onCompletion(MediaPlayer sdrPlayer) {
        Uri myUri5 = Uri.parse("http://dl.dropbox.com/u/24535120/Avicii%20clip%201.mp3");

        try {
            sdrPlayer.setDataSource(this, myUri5);
            sdrPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            sdrPlayer.prepare(); //don't use prepareAsync for mp3 playback
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        sdrPlayer.start();
    }
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
Splitusa
  • 1,181
  • 7
  • 31
  • 51

1 Answers1

4

You can register an OnCompletionListener with the media player. When it receives its callback notification, you need to call reset(), setDataSource, and prepare() on the media player for the next URL.

I don't believe there is anything in the api to tell you what data source a media player is using. You need to keep track of that yourself.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Thanks for the help Ted. So there is no way to have the title of the song appear when that song is playing from the URL? – Splitusa Jun 14 '11 at 16:07
  • Also, kind of a random question...Is it possible to create an array with muliple URLs inside and have the media player play the array? If it is possible, would i still have to set an on completion listener? thanks – Splitusa Jun 14 '11 at 16:10
  • There's nothing in the api that I know of to extract the song title. The only thing I can suggest is processing the mp3 stream yourself and then feeding it to the media player through a local file. There's experimental code to do that in Reuben Scratton's answer to [this query](http://stackoverflow.com/questions/5343730/mediaplayer-stutters-at-start-of-mp3-playback). As to your second question, I think you'll have to step through the url array yourself using an on completion listener. – Ted Hopp Jun 14 '11 at 17:28
  • hmmm ok. thanks for the link. So i have been trying with the code above and it is not playing the second url. any suggesstions? it just replays the first song – Splitusa Jun 14 '11 at 21:12
  • The idea would be that in your OnCompletionListener, you would go on to the next url. Your code always calls `setDataSource` with the same one. – Ted Hopp Jun 15 '11 at 02:51
  • I could be wrong, but can't you not use http://developer.android.com/reference/android/media/MediaPlayer.TrackInfo.html to get the track info? – dorien Jul 24 '12 at 09:19
  • @dorien - That can be used, but only if you're running Jelly Bean. It doesn't exist below API level 16. – Ted Hopp Jul 24 '12 at 15:32