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

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