Click here to Skip to main content
15,908,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In a project I'm working on, I have a JS file that should play a piece of music, and when that piece is done playing, the next one takes its place.

JavaScript
var soundtrack = {
 0: new Audio("./music/piece1.ogg"),
 2: new Audio("./music/piece2.ogg"),
 1: new Audio("./music/piece3.ogg"),
 3: new Audio("./music/piece4.ogg")
}
var currentTrack = 0;
function startMusic(){
 soundtrack[currentTrack].play();
 soundtrack[currentTrack].addEventListener("ended", function(){ nextSong()});
}
function nextSong(){
 currentTrack += 1;
 if(currentTrack == 4){
     currentTrack = 0;
 }
 soundtrack[currentTrack].play();
}
$( document ).ready(function() {
 setTimeout(function() { startMusic(); }, 15500);
});

Note: My use of the object "soundtrack" may be unconventional, but there was a bug using an array so I tried this and it worked.

The first piece plays through fine, and the next one starts up fine. About maybe 10 seconds in, the playing just stops.

Any help to solve this issue would be greatly appreciated, thanks!

Note: I am aware not all pieces will play after one other, I'm waiting until this issue is fixed before continuing.

What I have tried:

Reordering the pieces

Google

stackoverflow

Removing the event listener as someone suggested it may be the problem
Posted
Updated 30-Jul-16 5:26am

1 solution

Quote:
The first piece plays through fine, and the next one starts up fine. About maybe 10 seconds in, the playing just stops.
I don't know why second piece stops after 10 seconds, but I know a reason why it don't go to third piece.
In startMusic(), you add an event listener to piece 0 when you should add the event listener to every piece since you have 4 audio objects.
 
Share this answer
 
Comments
G4mm4R4y 30-Jul-16 14:31pm    
"Note: I am aware not all pieces will play after one other, I'm waiting until this issue is fixed before continuing."

If this wasn't clear enough, I apologize for the confusion
Patrice T 30-Jul-16 14:37pm    
Without the audio files it is impossible to reproduce your problem.
It is even possible that the second audio file is only 10 seconds or corrupted.
G4mm4R4y 30-Jul-16 14:53pm    
Although anything is possible, I doubt they are corrupted... See how the numbers are out of order? That's because I tried switching them to see if that's the case.

I'll try and get an .ogg extension for wmp so I can see if it's the files.
G4mm4R4y 30-Jul-16 14:59pm    
I've confirmed the audio is fine

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900