Skip to content

Instantly share code, notes, and snippets.

@mcongrove
Created April 20, 2011 06:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mcongrove/930506 to your computer and use it in GitHub Desktop.
Save mcongrove/930506 to your computer and use it in GitHub Desktop.
Retrieve the duration of an HTML5 audio element.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>HTML5 Audio Duration</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
var audio, duration;
window.onload = function() {
var element = document.createElement("audio");
element.setAttribute("id", "audio");
element.setAttribute("src", "http://watoo.net:8000/INTRODUCTION.mp3");
document.getElementsByTagName("body")[0].appendChild(element);
getDuration();
};
function getDuration() {
audio = document.getElementById("audio");
audio.addEventListener("loadedmetadata", function(_event) {
duration = audio.duration;
document.getElementsByTagName("body")[0].removeChild(audio);
});
}
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment