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

**URL:** <https://qa.fmod.com/t/sound-getlength-works-once-subsequently-gives-value-of-0/17157>\
**Category:** Unity\
**Tags:** unity, csharp\
**Created:** [May 5, 2021, 6:09am UTC](https://qa.fmod.com/t/sound-getlength-works-once-subsequently-gives-value-of-0/17157 "2021-05-05T06:09:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ADE921](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/ade921/32/2163_2.png) [@ADE921](https://qa.fmod.com/u/ADE921)\
**Post date:** [May 5, 2021, 6:09am UTC](https://qa.fmod.com/t/sound-getlength-works-once-subsequently-gives-value-of-0/17157/1 "2021-05-05T06:09:05Z")

</div>

Hello!

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](https://qa.fmod.com/t/get-length-duration-of-programmer-instrument-audio-table-loaded-sound/13911)), 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:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/e/ec3721a069462bba90e0dd787cd4f7b418d83ddf.jpeg)

---

<div class="post-metadata">

**Author:** ![mathew](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/mathew/32/431_2.png) [@mathew](https://qa.fmod.com/u/mathew)\
**Post date:** [May 7, 2021, 5:22am UTC](https://qa.fmod.com/t/sound-getlength-works-once-subsequently-gives-value-of-0/17157/2 "2021-05-07T05:22:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ADE921](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/ade921/32/2163_2.png) [@ADE921](https://qa.fmod.com/u/ADE921)\
**Post date:** [May 7, 2021, 6:27am UTC](https://qa.fmod.com/t/sound-getlength-works-once-subsequently-gives-value-of-0/17157/3 "2021-05-07T06:27:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mathew](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/mathew/32/431_2.png) [@mathew](https://qa.fmod.com/u/mathew)\
**Post date:** [May 7, 2021, 7:08am UTC](https://qa.fmod.com/t/sound-getlength-works-once-subsequently-gives-value-of-0/17157/4 "2021-05-07T07:08:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ADE921](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/ade921/32/2163_2.png) [@ADE921](https://qa.fmod.com/u/ADE921)\
**Post date:** [May 8, 2021, 6:38am UTC](https://qa.fmod.com/t/sound-getlength-works-once-subsequently-gives-value-of-0/17157/5 "2021-05-08T06:38:57Z")

</div>

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!
