Last active
December 15, 2015 19:19
-
-
Save tjosten/5310238 to your computer and use it in GitHub Desktop.
HTML 5 <audio> & JavaScript
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
if (typeof Audio != "undefined") { // make sure the browser knows about <audio> | |
if (navigator.userAgent.toLowerCase().indexOf("firefox") > -1) { // firefox does not play .mp3 due to licensing issues | |
var audioFile = "sound.ogg"; | |
} else { | |
var audioFile = "sound.mp3"; | |
} | |
var audioTag = new Audio(audioFile); | |
audioTag.load(); // if you want the file to be loaded right now, use this. otherwise, use it before .play() but be aware that the playback will be delayed then, of course | |
refreshAudioTag = function() { // workaround for strange chrome behaviour (without calling this, the file won't be played again after it has been played once.) | |
audioTag.src = audioFile; | |
}; | |
// whenever you want to start the playback, use this: | |
refreshAudioTag(); | |
audioTag.play(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment