gpt4 book ai didi

javascript - 如何使用Vanilla JavaScript自动播放下一首歌

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

嗨,我正在尝试在当前播放的歌曲结束时自动播放下一首歌曲。

我还想知道如何确保播放列表从最后一首歌曲结束时的开头开始。

谢谢 !

var songs = [
"song1.mp3",
"song2.mp3",
"song3.mp3",
"song4.mp3"
];

var song = new Audio();
var currentSong = 0;
var len = currentSong.length - 1;

function playSong() {
song.src = songs[currentSong];
song.play();
}

// play the next song when current song ends
song.addEventListener("ended", function playNextS() {
currentSong++;
if (currentSong == len) {
currentSong = 0;
playSong();
}
<div class="player">
<div class="songCover"><img src="" alt="" /></div>
<div class="about">
<div class="songTitle"></div>
<div class="Artist"></div>
</div>
</div>

最佳答案

试试这个,您没有以正确的方式使用数组

var songs = [
"song1.mp3",
"song2.mp3",
"song3.mp3",
"song4.mp3"
];

var song = new Audio();
var currentSong = 0;
var len = songs.length;

function playSong(index) {
song.src = songs[index];
song.play();
}
song.addEventListener("ended", function playNextS() {
currentSong++;
if (currentSong == len) {
currentSong = 0;
playSong(currentSong);
}
else{
playSong(currentSong);
}
<div class="player">
<div class="songCover"><img src="" alt="" /></div>
<div class="about">
<div class="songTitle"></div>
<div class="Artist"></div>
</div>
</div>

关于javascript - 如何使用Vanilla JavaScript自动播放下一首歌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58679447/

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