Last active
July 12, 2018 08:18
-
-
Save martijnluinstra/ba744b5c4742244d6b824a6b3441ee7a to your computer and use it in GitHub Desktop.
A simple JavaScript based soundboard
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
<!-- Example of index.html --> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>Soundboard</title> | |
<link rel="stylesheet" href="/css/style.css"> | |
</head> | |
<body> | |
<header id="title"> | |
<h1>Soundboard</h1> | |
</header> | |
<div id="sounds"></div> | |
<script src="/js/Soundboard.js"></script> | |
</body> | |
</html> |
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
/** | |
* Some javascript to render a soundboard. | |
* Demo: https://vakantieracer.com/ | |
* | |
* You may use an modify this code however you like for non-commercial purposes. | |
* I will appreciate it if you mention my name when you do so. | |
* | |
* Copyright (c) 2018 - Martijn Luinstra | |
*/ | |
/** | |
* Renders a single button (.sound), based on `sounds.json` and sources sound | |
* from the `/sounds` folder. | |
*/ | |
function renderButton (data) { | |
let soundButton = document.createElement('button'); | |
let soundName = document.createTextNode(data.name); | |
soundButton.appendChild(soundName); | |
soundButton.classList.add('sound'); | |
soundButton.addEventListener('click', () => { | |
let audio = new Audio('sounds/' + data.file); | |
audio.addEventListener('ended', () => soundButton.blur()); | |
audio.play(); | |
}); | |
return soundButton; | |
} | |
/** | |
* Render buttons into #sounds | |
*/ | |
function renderButtons (data) { | |
let soundsList = document.getElementById('sounds'); | |
for ( let sound of data ) | |
soundsList.appendChild(renderButton(sound)); | |
} | |
// Fetch contents of `sounds.json` | |
fetch('/sounds.json') | |
.then( response => response.json() ) | |
.then( renderButtons ) | |
.catch( err => console.log(err) ); |
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
/** | |
* Example of `sounds.json`. The sound files are located in `/sounds/<filename>`. | |
*/ | |
[ | |
{ | |
"name": "Sound name", | |
"file": "file.mp3" | |
}, | |
{ | |
"name": "Name with non-break\u00ADing spaces", | |
"file": "file_2.mp3" | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment