Hello,
I have a Localised Audio Table set up in FMOD to deal with dialogue. I’ve followed the Unity scripting example (https://www.fmod.com/resources/documentation-unity?version=2.2&page=examples-programmer-sounds.html) which works fine.
In order to close the dialogue box etc. after the dialogue has finished, I need to know when the audio ends, but can’t see how to do this.
I’ve tried invoking an event in DialogueEventCallback (in the DESTROY_PROGRAMMER_SOUND case) but this crashes Unity… from searching on the forums, this is because the callback is on a different thread.
I’ve tried waiting for the dialogueInstance created in PlayDialogue to have the STOPPED state (using dialogueInstance.getPlaybackState(out playbackState); ) but the state goes from STARTING to PLAYING, but then keeps the PLAYING state.
Am I doing something wrong/is there another way to detect when the sound ends?
Here’s my code:
public FMOD.Studio.EventInstance PlayDialogue(string key)
{
var dialogueInstance = FMODUnity.RuntimeManager.CreateInstance(voiceEvent);
// Pin the key string in memory and pass a pointer through the user data
GCHandle stringHandle = GCHandle.Alloc(key, GCHandleType.Pinned);
dialogueInstance.setUserData(GCHandle.ToIntPtr(stringHandle));
dialogueInstance.setCallback(voiceCallback);
dialogueInstance.start();
dialogueInstance.release();
return dialogueInstance;
}
voiceCallback is a copy of what’s in here: https://www.fmod.com/resources/documentation-unity?version=2.2&page=examples-programmer-sounds.html
I call this:
FMOD.Studio.EventInstance dialogueInstance = FMODManager.instance.PlayDialogue(line.line);
StartCoroutine(EndDialogue(dialogueInstance));
And:
IEnumerator EndDialogue(FMOD.Studio.EventInstance dialogueInstance)
{
FMOD.Studio.PLAYBACK_STATE playbackState = FMOD.Studio.PLAYBACK_STATE.STARTING;
while (playbackState != FMOD.Studio.PLAYBACK_STATE.STOPPED)
{
dialogueInstance.getPlaybackState(out playbackState);
print("Current state: " + playbackState);
yield return null;
}
//hide dialogue box
//stop character talking
//ETC
}
^^ this logs out STARTING about 3 times, then PLAYING until I’ve stopped Unity’s play mode.