简体   繁体   中英

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?)

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()

also change lenght to length

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...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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