Sky_Blue

for emoticon ^_^

Jun 30th, 2022
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. <audio id="music" src="https://files.catbox.moe/uqyniq.mp3"></audio>
  2. <button id="btn" onclick="playpause()">play</button>
  3.  
  4. <script>
  5. let player = document.getElementById("music"); //the audio id
  6. let btn = document.getElementById("btn"); //the button id
  7. isPlaying = false; //set to false because it is not playing yet
  8.  
  9. function playpause() {
  10. if (!isPlaying) play(); //if isPlaying is false, then play the music
  11. else pause(); //if isPlaying is true, then pause the music
  12. }
  13.  
  14. function play(){
  15. player.play(); //play the music
  16. isPlaying = true; //set to true because the music is playing
  17. btn.innerHTML = "pause"; //change the button to say pause
  18. }
  19.  
  20. function pause(){
  21. player.pause(); //pause the music
  22. isPlaying = false; //set to false because the music is not playing
  23. btn.innerHTML = "play"; //change the button to say play
  24. }
  25.  
  26. player.onended = function(){
  27. isPlaying = false; //the song has ended, isPlaying is false
  28. btn.innerHTML = "play"; //change the button to say play
  29. }
  30. </script>
Add Comment
Please, Sign In to add comment