Skip to content

Instantly share code, notes, and snippets.

@d-baker
Last active August 29, 2015 14:08
Show Gist options
  • Save d-baker/db38b16fe31126a654bd to your computer and use it in GitHub Desktop.
Save d-baker/db38b16fe31126a654bd to your computer and use it in GitHub Desktop.
HTML5 audio loading efficiency hack to be called on play()
// an array of the src attributes of every audio element on the page (assumes sources are already set, obviously)
var sources = [];
$("audio").each (function(i) {
$(this).attr("id", i.toString());
sources.push($(this).find("source").attr("src"));
});
// call this function when the user clicks the play button
// $target is a jQuery object containing the "path" to your audio elements in the dom
function hack($target) {
$("audio source").attr("src", "");
var id = parseInt($target.find("audio").attr("id"));
$target.find("audio source").attr("src", sources[id]);
$("audio").load();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment