简体   繁体   English

html5音频需要一些代码帮助

[英]Some code help needed with html5 audio

What is happening is it repeats some 1 over and over 发生的事情是它一遍又一遍地重复

Globals: 全球:

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];

Before game loop I call: 在游戏循环之前,我打电话:

song.play();

Listener: (I have feeling this stays with same var and does not move up as song points at new var in array?) 侦听器:(我感觉这与相同的var保持在一起,并且不会随着数组中新var上的歌曲点向上移动吗?)

song.addEventListener('ended', NextSong);   

it calls: 它调用:

function NextSong()
 {

currentSong++;

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

song.play();

}

change song.play() in your NextSong function to songList[currentsong].play() NextSong函数中的song.play()更改为songList[currentsong].play()

also change lenght to length 也改变lenghtlength

and you can make your function's code prettier 您可以使函数的代码更漂亮

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

EDIT 编辑

if you need to start loop all your songs. 如果您需要开始循环播放所有歌曲。 In your code it seams that first one is called once and only others are in loop (0, 1, 2, ..., 10, 1, 2, 3...) 在您的代码中,它表明第一个被调用一次,只有其他一个被循环(0、1、2,...,10、1、2、3 ...)

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

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