# How to retrieve a sound total time/length/duration?

**URL:** <https://qa.fmod.com/t/how-to-retrieve-a-sound-total-time-length-duration/19300>\
**Category:** Unity\
**Created:** [September 21, 2022, 7:28pm UTC](https://qa.fmod.com/t/how-to-retrieve-a-sound-total-time-length-duration/19300 "2022-09-21T19:28:06Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![zanardi](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/zanardi/32/1045_2.png) [@zanardi](https://qa.fmod.com/u/zanardi)\
**Post date:** [September 21, 2022, 7:28pm UTC](https://qa.fmod.com/t/how-to-retrieve-a-sound-total-time-length-duration/19300/1 "2022-09-21T19:28:06Z")

</div>

Hi there!  
I’m trying to figure out how to retrieve a sound total time. I’ve tested the following:  
soundState = FMODUnity.RuntimeManager.CreateInstance(soundKey);  
FMODUnity.RuntimeManager.AttachInstanceToGameObject(soundState, transform);  
soundState.start();  
FMOD.Studio.EventDescription evt;  
soundState.getDescription(out evt);  
int len = 0;  
evt.getLength(out len);

Unfortunately, len value ends with the value of 0 - zero. Am I using the wrong method for the task?

Thanks,

---

<div class="post-metadata">

**Author:** ![jeff\_fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/jeff_fmod/32/1766_2.png) [@jeff\_fmod](https://qa.fmod.com/u/jeff_fmod)\
**Post date:** [September 27, 2022, 5:06am UTC](https://qa.fmod.com/t/how-to-retrieve-a-sound-total-time-length-duration/19300/2 "2022-09-27T05:06:33Z")

</div>

`EventDescription::getLength` only works on events with a [timeline parameter](https://fmod.com/docs/2.02/studio/glossary.html#timeline), does the event in question have a timeline parameter?

---

<div class="post-metadata">

**Author:** ![zanardi](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/zanardi/32/1045_2.png) [@zanardi](https://qa.fmod.com/u/zanardi)\
**Post date:** [September 27, 2022, 7:49pm UTC](https://qa.fmod.com/t/how-to-retrieve-a-sound-total-time-length-duration/19300/3 "2022-09-27T19:49:20Z")

</div>

No, it does not. It’s good to know that the issue is there. The sound guy wants to keep it this way, so there may be sounds for Japanese VA, English VA.

What do you think? It would be better to try another way to retrieve a sound length, or another way for separating audio for localized versions?

---

<div class="post-metadata">

**Author:** ![jeff\_fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/jeff_fmod/32/1766_2.png) [@jeff\_fmod](https://qa.fmod.com/u/jeff_fmod)\
**Post date:** [September 27, 2022, 11:39pm UTC](https://qa.fmod.com/t/how-to-retrieve-a-sound-total-time-length-duration/19300/4 "2022-09-27T23:39:06Z")

</div>

If you want the length of a sound when it is played you can implement a callback on [`FMOD_STUDIO_EVENT_CALLBACK_SOUND_PLAYED`](https://fmod.com/docs/2.02/api/studio-api-eventinstance.html#fmod_studio_event_callback_type), which will give you the underlying `Sound` object, on which you can call [`Sound::getLength`](https://fmod.com/docs/2.02/api/core-api-sound.html#sound_getlength). We have an example of how to implement an event callback here: [Timeline Callbacks](https://fmod.com/docs/2.02/unity/examples-timeline-callbacks.html)  
You would need to add the `FMOD.Studio.EVENT_CALLBACK_TYPE.SOUND_PLAYED` flag when calling `EventInstance::setCallback` and then the callback would be something like this:

```ruby
[AOT.MonoPInvokeCallback(typeof(FMOD.Studio.EVENT_CALLBACK))]
static FMOD.RESULT SoundCallback(FMOD.Studio.EVENT_CALLBACK_TYPE type, System.IntPtr instancePtr, System.IntPtr parameterPtr)
{
	FMOD.RESULT result;
	FMOD.Studio.EventInstance instance = new FMOD.Studio.EventInstance(instancePtr);
		
	switch (type)
	{
		case FMOD.Studio.EVENT_CALLBACK_TYPE.SOUND_PLAYED:
			{
				FMOD.Sound sound = new FMOD.Sound(parameterPtr);
				result = sound.getName(out string name, 256);
				if (result != FMOD.RESULT.OK)
					return result;

				result = sound.getLength(out uint length, FMOD.TIMEUNIT.MS);
				if(result == FMOD.RESULT.OK)
				    return result;

				Debug.Log("Sound Played. Name: " + name + " Length: " + length); 
				break; 
			}

	}
	return FMOD.RESULT.OK; 
}

```

---

<div class="post-metadata">

**Author:** ![ivyvyx116](https://avatars.discourse-cdn.com/v4/letter/i/e19b73/32.png) [@ivyvyx116](https://qa.fmod.com/u/ivyvyx116)\
**Post date:** [March 15, 2023, 10:27am UTC](https://qa.fmod.com/t/how-to-retrieve-a-sound-total-time-length-duration/19300/5 "2023-03-15T10:27:50Z")

</div>

Hi! Could you elaborate a bit on the example code given here?

Which lines are must-have in order to retrive the audio file length?  
I’m slightly confused with the switch case and the FMOD.RESULT return type here.

Would this also work if it’s a programmer instrument that calls a different wav file each time?

---

<div class="post-metadata">

**Author:** ![jeff\_fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/jeff_fmod/32/1766_2.png) [@jeff\_fmod](https://qa.fmod.com/u/jeff_fmod)\
**Post date:** [March 16, 2023, 3:36am UTC](https://qa.fmod.com/t/how-to-retrieve-a-sound-total-time-length-duration/19300/6 "2023-03-16T03:36:48Z")

</div>

> [@ivyvyx116](#):
>
> Which lines are must-have in order to retrive the audio file length?

The minimum amount of code would be:

```auto
[AOT.MonoPInvokeCallback(typeof(FMOD.Studio.EVENT_CALLBACK))]
static FMOD.RESULT SoundCallback(FMOD.Studio.EVENT_CALLBACK_TYPE type, System.IntPtr instancePtr, System.IntPtr parameterPtr)
{
	FMOD.Studio.EventInstance instance = new FMOD.Studio.EventInstance(instancePtr);
	FMOD.Sound sound = new FMOD.Sound(parameterPtr);
	return sound.getLength(out uint length, FMOD.TIMEUNIT.MS);
}

```

> [@ivyvyx116](#):
>
> I’m slightly confused with the switch case and the FMOD.RESULT return type here.

This is inside a generic callback, which might not necessarily be `SOUND_PLAYED` every time. If you look at our [Programmer Sounds](https://fmod.com/docs/2.02/unity/examples-programmer-sounds.html) example you will see the relevance of the switch.  
As for the `FMOD.RESULT` return type, every function in the FMOD API returns an `FMOD.RESULT`, telling you whether the operation was successful. This can be used for fault finding and recovering from errors. A requirement of [Event Callbacks](https://fmod.com/docs/2.02/api/studio-guide.html#event-callbacks) is that they return an `FMOD.RESULT`, which is why it has `return FMOD.RESULT.OK` at the bottom of the callback, to notify the internal system that the callback completed successfully.

> [@ivyvyx116](#):
>
> Would this also work if it’s a programmer instrument that calls a different wav file each time?

Yes. The [`FMOD.Sound`](https://fmod.com/docs/2.02/api/core-api-sound.html) object will be wrapping a single audio file in the callback. If your event has two audio files in it, then this callback will be triggered twice, once for each audio file when it begins playing.

---

<div class="post-metadata">

**Author:** ![ivyvyx116](https://avatars.discourse-cdn.com/v4/letter/i/e19b73/32.png) [@ivyvyx116](https://qa.fmod.com/u/ivyvyx116)\
**Post date:** [March 16, 2023, 9:36am UTC](https://qa.fmod.com/t/how-to-retrieve-a-sound-total-time-length-duration/19300/7 "2023-03-16T09:36:38Z")

</div>

Thanks a lot for your explanation!

I’m still not quite sure where to place the sound.getLength() command.

After checking the Programmer Sounds example, which we are using, I ended up placing the sound.getLength() calls in both if-else cases under the CREATE\_PROGRAMMER\_SOUND case, with the “dialogueSound” Sound var.

In order to use the returned length, I initiated an uint variable outside the function and passed it in as the “out”. However, when trying to print this return value, it is still always 0.  
Even after trying with a temp var as shown in example, the printed result is still 0.  
There is always a dialogue file playing, so it shouldn’t be 0, I assume.

What am I doing or understanding wrong here?  
(Hopefully my description makes sense.)

---

<div class="post-metadata">

**Author:** ![jeff\_fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/jeff_fmod/32/1766_2.png) [@jeff\_fmod](https://qa.fmod.com/u/jeff_fmod)\
**Post date:** [March 16, 2023, 11:04pm UTC](https://qa.fmod.com/t/how-to-retrieve-a-sound-total-time-length-duration/19300/8 "2023-03-16T23:04:58Z")

</div>

It sounds like you are using global data, which is not the correct way to pass data in and out of a static callback- you should be using [`Studio.EventInstance.setUserData`](https://fmod.com/docs/2.02/api/studio-api-eventinstance.html#studio_eventinstance_setuserdata) / [`Studio.EventInstance.getUserData`](https://fmod.com/docs/2.02/api/studio-api-eventinstance.html#studio_eventinstance_getuserdata) to pass data in and out of the callback.  
Can you please share a code snippet of your callback?

---

<div class="post-metadata">

**Author:** ![ivyvyx116](https://avatars.discourse-cdn.com/v4/letter/i/e19b73/32.png) [@ivyvyx116](https://qa.fmod.com/u/ivyvyx116)\
**Post date:** [March 16, 2023, 11:59pm UTC](https://qa.fmod.com/t/how-to-retrieve-a-sound-total-time-length-duration/19300/9 "2023-03-16T23:59:18Z")

</div>

For using that global data, since it’s attached to an instance of a singleton, it should be working I assume?

Our code snippet is identical to the example here in [Programmer Sounds](https://fmod.com/docs/2.02/unity/examples-programmer-sounds.html). There is no line number here, but it’s just in the last DialogueEventCallback() function, under the first CREATE\_PROGRAMMER\_SOUND case, in both “if” and “else” cases, after the if (soundResult == FMOD.RESULT.OK) braces.

---

<div class="post-metadata">

**Author:** ![jeff\_fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/jeff_fmod/32/1766_2.png) [@jeff\_fmod](https://qa.fmod.com/u/jeff_fmod)\
**Post date:** [March 20, 2023, 1:02am UTC](https://qa.fmod.com/t/how-to-retrieve-a-sound-total-time-length-duration/19300/10 "2023-03-20T01:02:33Z")

</div>

> [@ivyvyx116](#):
>
> For using that global data, since it’s attached to an instance of a singleton, it should be working I assume?

The correct way to use external data in the callback API is to pass it as user data into [`Studio.EventInstance.setUserData`](https://fmod.com/docs/2.02/api/studio-api-eventinstance.html#studio_eventinstance_setuserdata) and retrieve it with [`Studio.EventInstance.getUserData`](https://fmod.com/docs/2.02/api/studio-api-eventinstance.html#studio_eventinstance_getuserdata). It might be fine in your case to do otherwise, but you can hit easily run into unexpected behavior and obscure crashes due to memory management issues, so it is best to get/set the user data as recommended.  
As for whether that is actually the cause of your problem I am not sure- I have added these lines to our example and it is working fine:

```csharp
[AOT.MonoPInvokeCallback(typeof(FMOD.Studio.EVENT_CALLBACK))]
static FMOD.RESULT DialogueEventCallback(FMOD.Studio.EVENT_CALLBACK_TYPE type, IntPtr instancePtr, IntPtr parameterPtr)
{
...
    FMOD.Sound dialogueSound;
    var soundResult = FMODUnity.RuntimeManager.CoreSystem.createSound(Application.streamingAssetsPath + "/" + userData.Key, soundMode, out dialogueSound);
    if (soundResult == FMOD.RESULT.OK)
    {
        parameter.sound = dialogueSound.handle;
        parameter.subsoundIndex = -1;
        Marshal.StructureToPtr(parameter, parameterPtr, false);
    }

    var result = dialogueSound.getName(out string name, 256);
    if (result != FMOD.RESULT.OK)
        return result;

    result = dialogueSound.getLength(out uint length, FMOD.TIMEUNIT.MS);
    if (result != FMOD.RESULT.OK)
        return result;

    Debug.Log("Sound Played. Name: " + name + " Length: " + length);
...
}

```

What version of FMOD are you using?

---

<div class="post-metadata">

**Author:** ![ivyvyx116](https://avatars.discourse-cdn.com/v4/letter/i/e19b73/32.png) [@ivyvyx116](https://qa.fmod.com/u/ivyvyx116)\
**Post date:** [March 30, 2023, 10:17am UTC](https://qa.fmod.com/t/how-to-retrieve-a-sound-total-time-length-duration/19300/11 "2023-03-30T10:17:20Z")

</div>

We are using FMOD v 2.02.07.

In the example, we have very similar code to what you have up there. The code didn’t print anything to the console, so I moved the `Debug.Log` line before checking the result. This time the line is printed, indicating that the `result` just now is not OK.

Then I tried printing the result - `ERR_NOTREADY`.

We also tried placing the following code outside the switch case:

```auto
FMOD.Sound dialogueSound1 = new FMOD.Sound(parameterPtr); 
dialogueSound1.getLength(out uint length, FMOD.TIMEUNIT.MS);
Debug.Log(length);

```

Placed before or after the switch case, this would give us a number, but it’s always the length of the previous file played. Therefore we still cannot really apply this value.

Any leads on how to solve this issue with the “not ready” error?

---

<div class="post-metadata">

**Author:** ![jeff\_fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/jeff_fmod/32/1766_2.png) [@jeff\_fmod](https://qa.fmod.com/u/jeff_fmod)\
**Post date:** [April 2, 2023, 11:13pm UTC](https://qa.fmod.com/t/how-to-retrieve-a-sound-total-time-length-duration/19300/12 "2023-04-02T23:13:04Z")

</div>

> [@ivyvyx116](#):
>
> ERR\_NOTREADY

This indicates your `Sound` has not loaded yet. Can you please query the `Sound`’s open state with [`Sound::getOpenState`](https://fmod.com/docs/2.02/api/core-api-sound.html#sound_getopenstate) and tell me which [`FMOD_OPENSTATE`](https://fmod.com/docs/2.02/api/core-api-sound.html#fmod_openstate) it is reporting?
