TheTintin

Untitled

Feb 27th, 2021
985
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const DATA = [
  2.   {
  3.     "title": "jump",
  4.     "src": "http://commondatastorage.googleapis.com/codeskulptor-assets/jump.ogg"
  5.   },
  6.   {
  7.     "title": "paza-moduless",
  8.     "src": "http://codeskulptor-demos.commondatastorage.googleapis.com/pang/paza-moduless.mp3"
  9.   },
  10.   {
  11.     "title": "thrust",
  12.     "src": "http://commondatastorage.googleapis.com/codeskulptor-assets/sounddogs/thrust.ogg"
  13.   }
  14. ]
  15.  
  16. const soundsElement = document.querySelector("#sounds");
  17.  
  18. (async () => {
  19.   const sounds = await getSounds();
  20.   addSoundsToPage(sounds);
  21. })();
  22.  
  23. async function getSounds() {
  24.   return DATA;
  25. }
  26.  
  27. let prevplayer;
  28.  
  29. function addSoundsToPage(sounds) {
  30.   sounds.forEach((sound) => {
  31.     const soundDiv = document.createElement("div");
  32.     soundDiv.className = "sound";
  33.     const soundTitle = document.createElement("h2");
  34.     soundTitle.textContent = sound.title;
  35.     soundDiv.appendChild(soundTitle);
  36.  
  37.     const player = document.createElement("audio");
  38.     player.setAttribute("src", sound.src);
  39.     soundDiv.appendChild(player);
  40.  
  41.     soundDiv.addEventListener("mousedown", () => {
  42.       soundDiv.style.backgroundColor = "lightgray";
  43.       if (prevplayer) prevplayer.pause();
  44.       player.currentTime = 0;
  45.       player.play();
  46.       prevplayer = player;
  47.     });
  48.  
  49.     soundDiv.addEventListener("mouseup", () => {
  50.       soundDiv.style.backgroundColor = "";
  51.     });
  52.  
  53.     soundsElement.appendChild(soundDiv);
  54.   });
  55. }
  56.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×