# Detecting when a Localised Audio Table's programmer sound ends

**URL:** <https://qa.fmod.com/t/detecting-when-a-localised-audio-tables-programmer-sound-ends/17102>\
**Category:** Unity\
**Tags:** unity\
**Created:** [April 23, 2021, 1:05pm UTC](https://qa.fmod.com/t/detecting-when-a-localised-audio-tables-programmer-sound-ends/17102 "2021-04-23T13:05:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![JenFoxTor](https://avatars.discourse-cdn.com/v4/letter/j/67e7ee/32.png) [@JenFoxTor](https://qa.fmod.com/u/JenFoxTor)\
**Post date:** [April 23, 2021, 1:05pm UTC](https://qa.fmod.com/t/detecting-when-a-localised-audio-tables-programmer-sound-ends/17102/1 "2021-04-23T13:05:12Z")

</div>

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](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](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.

---

<div class="post-metadata">

**Author:** ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)\
**Post date:** [April 28, 2021, 5:45am UTC](https://qa.fmod.com/t/detecting-when-a-localised-audio-tables-programmer-sound-ends/17102/2 "2021-04-28T05:45:40Z")

</div>

> [@JenFoxTor](#):
>
> `dialogueInstance.release();`

The problem is that you are releasing the instance:

> [FMOD - Studio API Reference | Studio::EventInstance (studio eventinstance release)](https://www.fmod.com/resources/documentation-api?version=2.2&page=studio-api-eventinstance.html#studio_eventinstance_release)  
> Event instances marked for release are destroyed by the asynchronous update when they are in the stopped state

You should only release an instance once your are finished with it, outside of callbacks you won’t be able to assume that it is still valid. Instead you could use the `EndDialogue` function to hide the box and release the instance once you are finished with it.

---

<div class="post-metadata">

**Author:** ![JenFoxTor](https://avatars.discourse-cdn.com/v4/letter/j/67e7ee/32.png) [@JenFoxTor](https://qa.fmod.com/u/JenFoxTor)\
**Post date:** [April 29, 2021, 9:09am UTC](https://qa.fmod.com/t/detecting-when-a-localised-audio-tables-programmer-sound-ends/17102/3 "2021-04-29T09:09:10Z")

</div>

Thanks - I admit I copied the example and didn’t look into each line of it! 🙂

Releasing the instance after I’ve finished with it does allow the playback state to change to STOPPING and STOPPED, but **way** after the audio finishes.

As an example, my first line of dialogue should end at about timeline position 3100 (I’ve logged that out too), but the playback state doesn’t change until 10,005. The .wav file does end as soon as the speech is finished.

Any ideas what the problem could be?

---

<div class="post-metadata">

**Author:** ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)\
**Post date:** [April 29, 2021, 10:06pm UTC](https://qa.fmod.com/t/detecting-when-a-localised-audio-tables-programmer-sound-ends/17102/4 "2021-04-29T22:06:29Z")

</div>

I would check the length of the event in Studio, if there are instruments on the timeline then the event won’t stop until it has finished playing everything on the timeline.

---

<div class="post-metadata">

**Author:** ![JenFoxTor](https://avatars.discourse-cdn.com/v4/letter/j/67e7ee/32.png) [@JenFoxTor](https://qa.fmod.com/u/JenFoxTor)\
**Post date:** [April 30, 2021, 2:47pm UTC](https://qa.fmod.com/t/detecting-when-a-localised-audio-tables-programmer-sound-ends/17102/5 "2021-04-30T14:47:15Z")

</div>

It’s just dialogue (a single Programmer instrument) - there are no other instruments.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/f/ffee7a259df59fc5848b5533280450768a78a16e.png)

---

<div class="post-metadata">

**Author:** ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)\
**Post date:** [May 2, 2021, 10:57pm UTC](https://qa.fmod.com/t/detecting-when-a-localised-audio-tables-programmer-sound-ends/17102/6 "2021-05-02T22:57:24Z")

</div>

That instrument is 10 seconds long on the timeline, if it is not set to ‘async’ then each time you play the event it will play for a minimum of 10 seconds.

---

<div class="post-metadata">

**Author:** ![JenFoxTor](https://avatars.discourse-cdn.com/v4/letter/j/67e7ee/32.png) [@JenFoxTor](https://qa.fmod.com/u/JenFoxTor)\
**Post date:** [May 3, 2021, 2:32pm UTC](https://qa.fmod.com/t/detecting-when-a-localised-audio-tables-programmer-sound-ends/17102/7 "2021-05-03T14:32:04Z")

</div>

> [@cameron-fmod](#):
>
> s 10 seconds long on the timeline, if it is not set to ‘async’ then each time you play the event it will play for a minimum of 10 seco

I was already set to async.

I’ve managed to fix it by deleting the programmer instrument, then adding a new one!

Thanks for your help.
