Toggling 2 musics with a checkbox

Help.
Im Trying To Get The 2 Musics To Stop When The Checkbox Is checked = false.
And If The Checkbox Is Checked = true To Play Only One At A Time Till One Ends To Play Another Music To Loop.

Heres The Code

var backgroundAudio1 = new Audio(“./assets/sound/music1.mp3”);
var backgroundAudio2 = new Audio(“./assets/sound/music2.mp3”);
backgroundAudio1.onended = function() {
console.log(‘Playing background audio’)
backgroundAudio2.play();
};
backgroundAudio2.onended = function() {
console.log(‘Playing background audio’)
backgroundAudio1.play();
};
function play() {
if(document.getElementById(“myCheck”).checked = true)
backgroundAudio1.play = true;
} else {
backgroundAudio1.pause = true;
backgroundAudio2.pause = true;
}

a few mistakes to sort out first:

  1. use “preformatted text” to post code snippets, this icon
    image
  2. the { and } are just… overall done wrong in the snippet. one thing you can do to help make sure you use the right number of each in the right places is to indent blocks such as the body of a function and the branches of if statements. then you make sure that you see the right { or } where the indentation changes. and you’ll be able to preserve the indentation when you paste code here in a preformatted text block. although using indentation is hard to do if you’re programming on a platform that doesn’t have “Tab” key.
  3. it looks like you haven’t finished learning how to control audio. you have some lines that try to call methods on an audio object, e.g. backgroundAudio1.play() while some assign a field, e.g. backgroundAudio1.play = true. see if you can look up which one is right.
  4. = true in the if condition does assignment rather than comparison. correct that before you go crazy :laughing:
  5. not objectively a mistake, but comparing .checked to true is redundant

try sorting these out before you get too deep in designing the logic for this music subsystem so that you can more effectively test out your ideas

1 Like

Also, try changing using the .play() and .pause() functions instead of defining them like that.