媒体播放器流网址,然后播放下一个网址

时间:2011-06-14 03:15:43

标签: android audio streaming media-player filenames

我目前有一个播放mp3文件的媒体播放器。当该文件完成后,代码是什么,以便它转到下一个url / mp3?

还有,是否有代码来获取文件的名称并显示它?我该怎么做呢?

谢谢

修改

请参阅下面的代码:

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();
    }
}

1 个答案:

答案 0 :(得分:4)

您可以在媒体播放器中注册OnCompletionListener。收到回叫通知后,您需要在媒体播放器上调用reset()setDataSourceprepare()以获取下一个网址。

我不相信api中有任何内容可以告诉您媒体播放器使用的数据源。你需要自己跟踪它。