Last active
August 29, 2015 14:08
-
-
Save d-baker/db38b16fe31126a654bd to your computer and use it in GitHub Desktop.
HTML5 audio loading efficiency hack to be called on play()
This file contains hidden or 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
// 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