Skip to content

Instantly share code, notes, and snippets.

@tjosten
Last active December 15, 2015 19:19
Show Gist options
  • Save tjosten/5310238 to your computer and use it in GitHub Desktop.
Save tjosten/5310238 to your computer and use it in GitHub Desktop.
HTML 5 <audio> & JavaScript
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