mytrack.duration returns NaN

2

Is this returning NaN, does anyone have a solution? Here is the code:

    <html>
<head>
    <title>teste</title>
    <meta charset="utf-8"/>
</head>
<body>
<audio id="mytrack" controls>
        <source src="Jinxed.mp3" type="audio/mp3">
</audio>
<script type="text/javascript">
alert(mytrack.duration);
</script>
</body>
</html>
    
asked by anonymous 28.04.2015 / 23:20

1 answer

2

Try to add the event loadeddata , example:

mytrack.addEventListener("loadeddata", function() {
    alert(this.duration);
});

The way you're trying to do, the script runs before the audio has loaded, so it returns NaN . With event loadeddata it executes when data is available.

HTML Audio / Video DOM loadeddata Event

JSFiddle Example

    
28.04.2015 / 23:58