带有< audio>的播放列表的JavaScript [英] playlist with <audio> JavaScript
本文介绍了带有< audio>的播放列表的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个个人小型网站,我想在其中播放一些歌曲.我试图做的是以下
I'm creating a personal small Website and i want to play some songs on it. what i've tried to do is the following
<script type="text/javascript">
var i=1;
var nextSong= "";
var audioPlayer = document.getElementById('audio');
audioPlayer.onended = function(){
i++
nextSong = "Music/"+i+".mp3";
audioPlayer.src = nextSong;
audioPLayer.load();
audioPlayer.play();
if(i == 37) // this is the end of the songs.
{
i = 1;
}
}
</script>
<audio id="audio" src="Music/1.mp3"controls="controls" autoplay="autoplay" align=""> </audio>
但是我无法使它正常工作,它只播放第一首歌曲,并且不执行我曾尝试提醒i状态的JS,但是它什么也没做.
however i can't get this to work , it just plays the first song and doesn't execute the JS i've tried alerting the state of i for example but it doesn't do anything.
推荐答案
尝试一下:
<script type="text/javascript">
var i=1;
var nextSong= "";
function setup() {
document.getElementById('audio').addEventListener('ended', function(){
i++;
nextSong = "Music/"+i+".mp3";
audioPlayer = document.getElementById('audio');
audioPlayer.src = nextSong;
audioPLayer.load();
audioPlayer.play();
if(i == 37) // this is the end of the songs.
{
i = 1;
}
}, false);
}
</script>
<body onLoad="setup();">
<audio id="audio" src="Music/1.mp3"controls="controls" autoplay="autoplay" align=""> </audio>
</body>
这篇关于带有< audio>的播放列表的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文