Skip to content

Instantly share code, notes, and snippets.

@msuchodolski
Created November 22, 2017 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msuchodolski/6748c65eaab07d42bfbcc50772385218 to your computer and use it in GitHub Desktop.
Save msuchodolski/6748c65eaab07d42bfbcc50772385218 to your computer and use it in GitHub Desktop.
Quick audio player in HTML5
<!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