gpt4 book ai didi

java - 当我在 android 中单击按钮时,我想一个接一个地播放音频

转载 作者:行者123 更新时间:2023-12-04 02:29:48 25 4
gpt4 key购买 nike

ArrayList<String> videolist = new ArrayList<>();
videolist.add("http://muurl.com/abc/song1.mp3");
videolist.add("http://muurl.com/abc/song2.mp3");
videolist.add("http://muurl.com/abc/song3.mp3");

我已将音频链接存储在数组列表中。当我点击按钮时,我想一个接一个地播放那个音频,当我点击按钮时,第二次音频应该从第一个音频链接开始

最佳答案

看看 MediaPlayerOnCompletionListener 一起上课:

你会做这样的事情:

int playListPos = 0; // declare this outside the button click probably as a global variable (so we can access it and increment in the on click listener of the button

// the below code should go inside the button click
String url = videolist.get(playListPos); // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioAttributes(
AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_MEDIA)
.build()
);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
// this will be called every time a file is finished playing
if (videolist.size() < playListPos) { // let's see if there is more files to play
mediaPlayer.setDataSource(videolist.get(playlistPos));
mediaPlayer.prepare();
mediaPlayer.start();
playListPos++;
} else {
// we played until the end. reset to 0 for when button is clicked again to restart from the beginning
playListPos = 0;
}
});
mediaPlayer.start();

关于java - 当我在 android 中单击按钮时,我想一个接一个地播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64963481/

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