mytrack.Duration returns Nan

Asked

Viewed 53 times

2

Are you returning Nan, does anyone have a solution? Follows 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>

1 answer

2


Try to add the event loadeddata, example:

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

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

HTML Audio/Video DOM loadeddata Event

Jsfiddle example

  • Did Nan return both here in my code and yours in Jsfiddle? Is it the browser?

  • @Asurakhan tested again and is returning NaN, more is because for some reason the mp3 file I put as an example in fiddle is not being located. :/

  • in my case it locates the audio, but it does not work and the codes are identical. Are 2 lines after all.

  • @Asurakhan see the issue, if this way works for your case.

  • Now it worked, but I didn’t understand anything. I know the eventlistener and his use with callback, but this one there.. I’m kind of dumb with "this" . I’d be grateful if you could explain it to me. Including why the other method doesn’t work.

  • @Asurakhan see the issue with the explanation.

  • Thank you very much.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.