# Stopping a sound dynamically

**URL:** <https://qa.fmod.com/t/stopping-a-sound-dynamically/11825>\
**Category:** Unity\
**Created:** [February 6, 2015, 11:56pm UTC](https://qa.fmod.com/t/stopping-a-sound-dynamically/11825 "2015-02-06T23:56:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chaostheorygames1](https://avatars.discourse-cdn.com/v4/letter/c/258eb7/32.png) [@chaostheorygames1](https://qa.fmod.com/u/chaostheorygames1)\
**Post date:** [February 6, 2015, 11:56pm UTC](https://qa.fmod.com/t/stopping-a-sound-dynamically/11825/1 "2015-02-06T23:56:38Z")

</div>

Is it possible to stop an EventInstance dynamically by calling FMOD\_StudioSystem.instance.getEvent("") on a sound that is already playing? I’m having trouble stopping any events with looping regions using EventInstance.stop() method.

I have several events with loop regions that are being played with this method:

```
    public bool Play(string name)
    {
        EventInstance ei = global_studio.GetEvent("event:/sfx/" + name);
        EventDescription ed = null;
        ei.getDescription(out ed);
        bool isOneShot = true;
        ed.isOneshot(out isOneShot);

        if (isOneShot)
        {
            global_studio.PlayOneShot("event:/sfx/" + name, Vector3.zero);
            return true;
        }
        else
        {
            if (CheckForErrors(ei.start()))
                return true;
        }

        return false;
    }

```

where the event instance is being discarded after it is played. Then the event is attempted to be stopped using this method:

```
    public void Stop(string name, bool allowFadeout = false)
    {
        EventInstance ei = global_studio.GetEvent("event:/sfx/" + name);

        PLAYBACK_STATE playback;
        CheckForErrors(ei.getPlaybackState(out playback));
        
        CheckForErrors(ei.stop(STOP_MODE.IMMEDIATE));
        Debug.log(name + " stopped");
        
        /*
        if (playback == PLAYBACK_STATE.PLAYING || playback == PLAYBACK_STATE.STARTING)
        {
            if (allowFadeout)
            {
                CheckForErrors(ei.stop(STOP_MODE.ALLOWFADEOUT));
                Debug.Log(name + " stopped");
            }
            else
            {
                CheckForErrors(ei.stop(STOP_MODE.IMMEDIATE));
                Debug.Log(name + " stopped");
            }
        }
        */

        CheckForErrors(ei.release());
    }

```

The stop method is being called successfully, and ei.stop() and ei.release() pass the ERRCHECK / CheckForErrors function with FMOD.Result.OK, but the sound continues to loop indefinitely in-game. It is as if a new instance of the event has begun playing. When using the block of code that has been commented out in the above method, PLAYBACK\_STATE is never found to be PLAYING or STARTING.

Do I have to store a reference to any looping sounds played using the EventInstance.start() method and then retrieve that reference before stopping them?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![nick1](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@nick1](https://qa.fmod.com/u/nick1)\
**Post date:** [February 8, 2015, 11:19pm UTC](https://qa.fmod.com/t/stopping-a-sound-dynamically/11825/2 "2015-02-08T23:19:43Z")

</div>

Yes you need to keep the instance you started to call stop on it.

---

<div class="post-metadata">

**Author:** ![chaostheorygames1](https://avatars.discourse-cdn.com/v4/letter/c/258eb7/32.png) [@chaostheorygames1](https://qa.fmod.com/u/chaostheorygames1)\
**Post date:** [February 9, 2015, 5:26am UTC](https://qa.fmod.com/t/stopping-a-sound-dynamically/11825/3 "2015-02-09T05:26:30Z")

</div>

> [@](#):
>
> Yes you need to keep the instance you started to call stop on it.

Reconfigured my Play() and Stop() functions to reference a Dictionary\<string, EventInstance\> while still being able to stop events by name and it works perfectly.  
Many thanks, Nicholas.
