简体   繁体   English

如何使用香草 javascript 自动播放下一首歌曲

[英]How to auto play next song with vanilla javascript

Hi I am trying to automatically play the next song when the current playing song is over.嗨,我正在尝试在当前播放歌曲结束时自动播放下一首歌曲。

I also wanted to know how I could make sure the playlist starts from the beginning when the last song ends.我还想知道如何确保播放列表在最后一首歌结束时从头开始。

Thank you !谢谢 !

 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>

Try this, Your are not using array in correct way试试这个,你没有以正确的方式使用数组

 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM