Jump to content

Sound element duration is 0


Anderberg
 Share

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

 Share

  • Recently Browsing   0 members

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