Sound.getLength() Works Once, Subsequently Gives Value of "0"


I am trying to get the length of an audio clip in an audio table that is playing back though a programmer instrument. After reviewing the API documentation and with some help from a forum post (Get length/duration of Programmer Instrument audio table loaded sound), I came up with this:

FMOD.Sound voSound;
var soundResult = FMODUnity.RuntimeManager.CoreSystem.createSound(voSoundInfo.name_or_data, soundMode | voSoundInfo.mode, ref voSoundInfo.exinfo, out voSound);

FMOD.Sound subSound;
voSound.getSubSound(voSoundInfo.subsoundindex, out subSound);
subSound.getLength(out uint length, FMOD.TIMEUNIT.MS);

The first time the audio plays back, the value assigned to “length” is the audio clip length (as expected). However, on subsequent playbacks (of any clip), the value assigned to “length” is 0.

Any suggestions on how to improve this code to return the audio clip length every time the audio plays back would be greatly appreciated! Here is a little more of the surrounding code for reference:

I suspect you are receiving FMOD_ERR_NOTREADY from some of those functions, can you verify by checking the results from each API call?

Thanks for the reply @mathew!

It looks like the result of voSound.getSubSound(voSoundInfo.subsoundindex, out subSound); (line 239) is ERR_NOTREADY.

The result of subSound.getLength(out uint length, FMOD.TIMEUNIT.MS); (line 240) is ERR_INVALID_PARAM.

The rest seem to be giving the OK result.

You’ll get FMOD_ERR_NOTREADY if you calling System::createSound with FMOD_NONBLOCKING in the mode parameter. Make sure to not have that value if you want things to operate synchronously. If you’re only interested in the length you can avoid the cost of loading the asset by passing in the FMOD_OPENONLY flag.

Thanks again for your help @mathew!

I ending up adding some additional code that made use of the FMOD_OPENONLY flag, and it worked like a charm!