Stopping an instanced loop is not working

Hello,
first of all I am new to FMOD so this might be a noob mistake:
I am using FMOD in Unity.
I want to instantiate an event that loops an alarm sound. This is happening in the Start() method of a prefab. This seems to work. The alarm is ringing. Once the player reaches to the alarming device and turns it off, the instance should be stopped immediatley. When I turn it off however, the sound doesn’t stop.
The debug messages are printed out correctly though. What am I doing wrong? Here is my starting and stopping code:

private FMOD.Studio.EventInstance alarmSoundInstance;

private void Start()
{
    //Start Alarm Sound
    StartAlarm(alarmSoundInstance);
}

public void StartAlarm(FMOD.Studio.EventInstance instance)
{
instance =   FMODUnity.RuntimeManager.CreateInstance(SoundManager.Instance.MeasurementDeviceAlarmEvent);
    instance.set3DAttributes(FMODUnity.RuntimeUtils.To3DAttributes(gameObject));
    instance.start();
    Debug.Log("ALARM STARTED");
}

public void StopAlarm(FMOD.Studio.EventInstance instance)
{
    instance.stop(FMOD.Studio.STOP_MODE.IMMEDIATE);
    instance.release();
    Debug.Log("ALARM STOPPED");
}

Thanks in advance!

The code you have shown seems fine, it could be simply how StopAlarm() is called in the game. Are you able to get a small Unity project of this happening and send it over for us to test on our end?

Sorry I do not have time to get a Unity project done since we are a very small team with a tight deadline. I fixed it by using the event emitter component instead for now. I’m going to post another solution here once I’ve got time to figure it out.

For some reason I’m having the same problem. I have this looped sound that I stop and you can hear it in the background. I have a registry on Event Instances and after I release them I take the Event Instance out of the registry, so I can see that the registry is empty but the sound can still be heard.

Unity 2020.3.27 FMOD (Unity) 2.01.07 FMOD Studio 2.01.05

Any clue?

I’m not able to replicate the issue on my machine using a looping event. Could you post how are you creating/releasing this event?

Unfortunately I have the same Issue. The FMOD.Studio.STOP_MODE.ALLOWFADEOUT is not working to stop a looping region. Im unsure why. At the moment I had to use a command instrument to stop the loop.

Hi,

What version of the FMOD integration are you using?

With the logging level set to Warning :
image
Are there any logs that you can share?

Just to confirm, you are calling stop() on a valid event instance?

Hello! I think I’m also having the same issue. There is a button in the scene where if you click on it, it takes you back to the previous scene and ends the music (using global variable 44, shown in code below). However, the music is not stopping. Here’s the full script I’m using to start the event.

I saw someone recommend adding the bottom part of the script to try to get a debug error to get more of an idea of what is going on, but I am not getting any console errors.

public class startApartmentMemoryMusic : MonoBehaviour
{
    public FMOD.Studio.EventInstance apartmentMemoryMusic;
    public string eventPath;
    GVar MyVar;
    GVar MyVar2;

    // Start is called before the first frame update
    float Start()
    {
        MyVar = AC.GlobalVariables.GetVariable(32);
        MyVar2 = AC.GlobalVariables.GetVariable(44);
        return 0f;

    }

    // Update is called once per frame
    void Update()
    {
        if (MyVar.BooleanValue == true)
        {
            apartmentMemoryMusic = FMODUnity.RuntimeManager.CreateInstance(eventPath);
            apartmentMemoryMusic.start();
            MyVar.BooleanValue = false;
        }

        if (MyVar2.BooleanValue == true)
        {
            apartmentMemoryMusic.stop(FMOD.Studio.STOP_MODE.IMMEDIATE);
            apartmentMemoryMusic.release();

            var result = apartmentMemoryMusic.stop(FMOD.Studio.STOP_MODE.IMMEDIATE);
            if (result != FMOD.RESULT.OK)
            {
                UnityEngine.Debug.Log($"Failed to stop event with result: {result}");
            }
            result = apartmentMemoryMusic.release();
            if (result != FMOD.RESULT.OK)
            {
                UnityEngine.Debug.Log($"Failed to stop event with result: {result}");
            }
        }
    }
}

Interestingly, here is another one of my scripts that works perfectly, and the sound stops upon variable change. The only difference that I can see is that this one is non looping in FMOD and the other is a looping track.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FMODUnity;
using FMOD;
using AC;

public class ComputerStartup : MonoBehaviour
{
    public FMOD.Studio.EventInstance computerStartup;
    public string eventPath;
    GVar MyVar;
    GVar MyVar1;

    // Start is called before the first frame update

    float Start()
    {
        MyVar = AC.GlobalVariables.GetVariable(29);
        MyVar1 = AC.GlobalVariables.GetVariable(31);
        return 0f;
    }


    // Update is called once per frame
    void Update()
    {
        if (MyVar.BooleanValue == true)
        {
            computerStartup = FMODUnity.RuntimeManager.CreateInstance(eventPath);
            computerStartup.start();
            MyVar.BooleanValue = false;
        }

        if (MyVar1.BooleanValue == true)
        {
            computerStartup.stop(FMOD.Studio.STOP_MODE.IMMEDIATE);
        }
    }
}

Greatly appreciate any insight anyone can provide! Also, .setPaused does not work either - I had eventually resorted to “ok well I won’t end the instance, i’ll just pause it” but even that did not work. thank you!

Hi,

Thank you for providing the codes.

Unfortunately, I am unable to reproduce the issue on my end in a similar situation.

Could you please provide the version number of the FMOD integration you are using?

Additionally, would it be possible for you to elaborate a bit more about how you set up the scene and how the two global variables function during the scene transition?

Hello! Thanks for your response. I am using Version: 2.02.20. Full disclosure I am very new to Unity and coding, so forgive any mistakes or obvious things that I miss.

I use Adventure Creator, which is a unity based game building toolkit. In adventure creator, you have a list of variables that you can check/set through visual scripting, or through code by using the GVar script above. In the Adventure Creator editor, you set their initial value. So, in the second script (the one that works) I have above for the computer startup, Variable29 is set to false at the beginning of the game, and once you click on the computer in my scene, it flips it to true. Then the script sees that variable 29 has changed and creates the instance.

I know that my issue does not lie within adventure creator/the variables, since it works for the computer startup. The computer startup sound only plays 1 time - it is not set to loop.

In the first script above for the startApartmentMemoryMusic, unless I’m missing something, these scripts are basically the same and should work the same way, yet the apartment memory music does not stop when variable 44/MyVar2 is flipped to true. This is the same case with all of my regions that are set to loop in FMOD.

Thanks for providing the version number and addtional details!

Could you please put in a UnityEngine.Debug.Log("Stop Music");to check whether the second if statement has been called?

if (MyVar2.BooleanValue == true)
{
+   UnityEngine.Debug.Log("Stop Music");`
    apartmentMemoryMusic.stop(FMOD.Studio.STOP_MODE.IMMEDIATE);
    apartmentMemoryMusic.release();

As it seems to me that nothing inside the loop has been called based on your description.

Also, just to confirm, are you using the script startApartmentMemoryMusic in both scenes?

Based on testing, it seems the problem might be related to using the same script across multiple scenes. Specifically when transitioning back to the previous scene, the apartmentMemoryMusic instance might not getting any reference linking to it as the created instance has only existed in the latter scene.

If that is the case, a potential solution is to ensure that the object containing the script is not destroyed when loading a new scene. This can be achieved by using DontDestroyOnLoad(this.gameObject); in the Awake() or Start() method of your script. This method makes the object persistent across scene changes, thus maintaining the instance references.

Let me know if it helps, please don’t hesitate to ask questions.

I put in that line you suggested and the message did populate in the console, so I knew the loop was being called.

Also, the whole thing is within the same scene - it didn’t have anything to do with scene changes/scripts getting destroyed etc.

However, what you said made me think through my issue a LOT more closely and I eventually found my issue. Two of my audio scripts basically conflicted and set a variable to an inappropriate state given what I was trying to accomplish. Everything works perfectly now! Basically, I just needed to go over everything with a fine tooth comb… but this thread made me think that there might have been an FMOD issue, but it was not. Thank you again!

2 Likes