@deron
You can toggle the pause/play functionality of an audio element using JavaScript by following these steps:
1
|
<audio id="myAudio" src="path/to/audio.mp3"></audio> |
1
|
<button id="playPauseButton">Play/Pause</button> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// Get the audio element and the play/pause button const myAudio = document.getElementById("myAudio"); const playPauseButton = document.getElementById("playPauseButton"); // Add a click event listener to the button playPauseButton.addEventListener("click", function() { // Check if the audio is currently paused if (myAudio.paused) { // If paused, play the audio myAudio.play(); playPauseButton.textContent = "Pause"; // Update button text to "Pause" } else { // If playing, pause the audio myAudio.pause(); playPauseButton.textContent = "Play"; // Update button text to "Play" } }); |
Now, when the button is clicked, it will either pause or play the audio, and the button text will update accordingly.