Jump to content

Sound element duration is 0


Recommended Posts

I have a situation where I need to know the duration of my sound elements before they are played. However, the duration is 0 (as well as durationMS and totalDuration). So I dug into the code and realised that duration is only set if the browser uses the audio tag, not when it uses the webaudio api (latest chrome use the latter). So what is the best way to get the duration?


I have done some digging:

Using the game.cache.getSound(key) you can extract information about the sound object. This object will hold a "data" property which has a "duration" property. But this is only set once the audio file has been decoded. The preload in a state does not wait for audio files to be decoded, which means that in the beginning of the create-state this data might not be available (aka when I need it to be).


Have anyone battled audio duration and succeeded?

Link to comment
Share on other sites

  • 3 years later...

This is a very old thread, but since it popped up on search (I'm having the same problem as OP but I'm pretty sure my sound is decoded <EDIT: That was solved as well :)>) I want to add that Sound.onDecoded sends a signal that you can use when you want to wait for a sound to be decoded.

Not sure if that was available on the time this thread was made, but for anyone searching, this is a thing.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...