How to make a button that pauses a background music

How To Make Multiple Background Musics Thats Toggleable
Right Now I Have This:

var music1 = new Sound(“./assets/sound/music1.mp3”, 0.5, 10);
var music2 = new Sound(“./assets/sound/music2.mp3”, 0.5, 10);
var music3 = new Sound(“./assets/sound/music3.mp3”, 0.5, 10);
musicstop = function() {
music1.pause();
music2.pause();
music3.pause();
}
music1 = function() {
music1.play();
music2.pause();
music3.pause();
};
music2 = function() {
music2.play();
music1.pause();
music3.pause();
};
music3 = function() {
music1.play();
music1.pause();
music2.pause();
};

Any Solution

Looking good at a pseudocode level. What’s new Sound in your snippet?

Idk What The New Sound Is

Its Nothing

Hi! I think new Sound doesn’t work. Try new Audio

Nah new Sound Works


I Just Dont Know How To Get Music To Play From A Button

Hi! I’m gonna make a glitch project to test everything for myself
Edit: Actually, quite easy

/*
  Your HTML:
  <button onclick="toggleAudio()">Toggle background audio</button>
*/
var backgroundAudio = new Audio(
  "https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3"
);
function toggleAudio() {
    return backgroundAudio.paused ? backgroundAudio.play() : backgroundAudio.pause();
}

1 Like

This Man Is Expert
Edit: Is It Possible To Make It A Checkbox?

1 Like

Hi! Yes it is! Try:

<input type="checkbox" onchange="toggleAudio()">
var backgroundAudio = new Audio(
  "https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3"
);
backgroundAudio.loop = true;
function toggleAudio() {
    return backgroundAudio.paused ? backgroundAudio.play() : backgroundAudio.pause();
}

Click the checkbox to enable and disable the background audio

1 Like

Thx For Your Help!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.