Created
November 22, 2017 18:38
-
-
Save msuchodolski/6748c65eaab07d42bfbcc50772385218 to your computer and use it in GitHub Desktop.
Quick audio player in HTML5
This file contains 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
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>audio test</title> | |
</head> | |
<body> | |
<audio controls="controls" autoplay> | |
Your browser does not support the <code>audio</code> element. | |
<source src="<%url_to_file%>" type="audio/wav"> | |
</audio> | |
<button class="playWAV">Play WAV</button> | |
<button class="playMP3">Play MP3</button> | |
<script> | |
/********************** | |
******** AUDIO ******** | |
**********************/ | |
// load and play a sound. | |
function playSound(path) { | |
// audio supported? | |
if (typeof window.Audio === 'function') { | |
var audioElem = new Audio(); | |
audioElem.src = path; | |
audioElem.play(); | |
} | |
} | |
// event listeners | |
document.querySelector('.playWAV').addEventListener('click', function(){ | |
playSound('<%url_to_file%>'); | |
}); | |
document.querySelector('.playMP3').addEventListener('click', function(){ | |
playSound('<%url_to_file%>'); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment