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

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,

EventDescription::getLength only works on events with a timeline parameter, does the event in question have a timeline parameter?

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?

If you want the length of a sound when it is played you can implement a callback on FMOD_STUDIO_EVENT_CALLBACK_SOUND_PLAYED, which will give you the underlying Sound object, on which you can call Sound::getLength. We have an example of how to implement an event callback here: Timeline Callbacks
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:

[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; 
}
1 Like