Stopping a sound dynamically

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.

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

1 Like

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.