Answer the question
In order to leave comments, you need to log in
How to loop music?
I have a Music class that plays music when the window is clicked. I have 2 tracks, they should start like this: the first track plays first, and after that the second track plays (looping). That is, after the first track, the second should be constantly repeated.
Here is the class:
export const soundVal = "50";
export let musicVal = "50";
export function changeVal(val: string) {
musicVal = val;
}
const musicStart = new Audio();
musicStart.src = "audio/start.mp3";
musicStart.volume = 0.5;
const musicMain = new Audio();
musicMain.src = "audio/main.mp3";
musicMain.volume = 0.5;
export class Music {
constructor() {
this.startMusicPlay = this.startMusicPlay.bind(this);
}
public changeMusicVolume(val: string) {
musicVal = val;
musicStart.volume = +val / 100;
musicMain.volume = +val / 100;
}
public start() {
window.addEventListener("click", this.startMusicPlay);
}
public startMusicPlay() {
musicStart.play();
musicStart.addEventListener("ended", this.startMainMusic);
window.removeEventListener("click", this.startMusicPlay);
}
private startMainMusic() {
musicMain.play();
musicMain.addEventListener("ended", this.startMainMusic);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question