Created
February 10, 2023 15:36
-
-
Save R3DHULK/236f63921c04be14aaab7bcce642a80b to your computer and use it in GitHub Desktop.
Add Music In Your Website
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You can add a toggle button or a checkbox to your website that allows the user to enable or disable the audio. Here's an example using a toggle button: | |
<audio id="music" src="yourmusic.mp3"></audio> | |
<button id="toggleBtn">Toggle Music</button> | |
<script> | |
var audio = document.getElementById("music"); | |
var toggleBtn = document.getElementById("toggleBtn"); | |
audio.playbackRate = 0.5; | |
audio.play(); | |
toggleBtn.addEventListener("click", function() { | |
if (audio.paused) { | |
audio.play(); | |
toggleBtn.innerHTML = "Pause Music"; | |
} else { | |
audio.pause(); | |
toggleBtn.innerHTML = "Play Music"; | |
} | |
}); | |
</script> | |
// In this example, the toggle button is created using a button element with an id of toggleBtn. The click event is attached to the button using addEventListener, and the function inside the event listener toggles the state of the audio between playing and pausing. The text of the button is updated to reflect the current state of the audio. | |
// You can also use a checkbox instead of a toggle button, like this: | |
<audio id="music" src="yourmusic.mp3"></audio> | |
<input type="checkbox" id="musicCheckbox">Play Music | |
<script> | |
var audio = document.getElementById("music"); | |
var checkbox = document.getElementById("musicCheckbox"); | |
audio.playbackRate = 0.5; | |
audio.play(); | |
checkbox.addEventListener("change", function() { | |
if (checkbox.checked) { | |
audio.play(); | |
} else { | |
audio.pause(); | |
} | |
}); | |
</script> | |
// In this example, the checkbox is created using an input element with a type of checkbox and an id of musicCheckbox. The change event is attached to the checkbox using addEventListener, and the function inside the event listener toggles the state of the audio based on the state of the checkbox. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment