gpt4 book ai didi

java - android媒体播放器流超过1个URL

转载 作者:行者123 更新时间:2023-12-01 12:56:20 26 4
gpt4 key购买 nike

我在 Android 应用程序中使用 mediaPlayer 从在线流式传输 MP3 url。我怎样才能流式传输 5 个网址来逐个播放,而不是只播放 1 个网址?这是我的代码

Uri myUri = Uri.parse("https://db.tt/9nBgouRf");


final MediaPlayer sdrPlayer = new MediaPlayer();



try {
sdrPlayer.setDataSource(this, myUri);
sdrPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
sdrPlayer.prepare(); //don't use prepareAsync for mp3 playback
} catch (IOException e) {

e.printStackTrace();
Toast.makeText(channelx.this,
"Please turn on WiFi and try again", Toast.LENGTH_LONG).show();
}




play.setOnClickListener( new OnClickListener() {

@Override
public void onClick(View v) {

sdrPlayer.start();


}
}

);

最佳答案

只需创建一个列表来保存所有 URI

设置一些类变量:

    private int playlistPos = 0;
private List<Uri> myUris = new ArrayList<Uri>();
private MediaPlayer sdrPlayer = new MediaPlayer();

设置初始化歌曲的方法:

    public initSong(Uri myUri) {
try {
sdrPlayer.setDataSource(this, myUri);
sdrPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
sdrPlayer.prepare(); // don't use prepareAsync for mp3 playback
}
catch (IOException e) {

e.printStackTrace();
Toast.makeText(channelx.this,
"Please turn on WiFi and try again",
Toast.LENGTH_LONG).show();
}
}

然后在onCreate()

    myUris.add(Uri.parse("https://db.tt/9nBgouRf"));
// Add the others as well...

initSong(myUris.get(playlistPos);

sdrPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
playlistPos++;
initSong(myUris.get(playlistPos));
sdrPlayer.start(); // Start it as well if you wish
}
});

play.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

sdrPlayer.start();

}
});

关于java - android媒体播放器流超过1个URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23858418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com