html5音频需要一些代码帮助

时间:2013-06-30 05:16:16

标签: javascript html5 audio

正在发生的事情是它一遍又一遍地重复一些

全局:

var song1 = new Audio("songs/loop1.mp3");
var song2 = new Audio("songs/loop2.mp3");
var song3 = new Audio("songs/loop3.mp3");
var song4 = new Audio("songs/loop4.mp3");
var song5 = new Audio("songs/loop5.mp3");
var song6 = new Audio("songs/loop6.mp3");
var song7 = new Audio("songs/loop7.mp3");
var song8 = new Audio("songs/loop8.mp3");
var song9 = new Audio("songs/loop9.mp3");
var song10 = new Audio("songs/loop10.mp3");
var song11 = new Audio("songs/loop11.mp3");
var songList = [];
var currentSong = 1;

songList.push(song1,song2,song3,song4,song5,song6,song7,song8,song9,song10,song11);

var song = songList[0];

在游戏循环之前,我打电话给:

song.play();

听众:(我觉得这种音乐保持相同的var并且不会因为歌曲指向数组中的新var而向上移动?)

song.addEventListener('ended', NextSong);   

它叫:

function NextSong()
 {

currentSong++;

if(currentSong > songList.lenght);
{
currentSong = 1;
}

song.play();

}

1 个答案:

答案 0 :(得分:1)

song.play()功能中的NextSong更改为songList[currentsong].play()

也将lenght更改为length

你可以使你的函数代码更漂亮

function nextSong{
    currentSong = (++currentSong)%songList.length;
    songList[currentsong].play();
}

修改

如果您需要开始循环播放所有歌曲。在你的代码中它接缝第一个被调用一次而只有其他被循环(0,1,2,...,10,1,2,3 ...)