Stop Playing OneShot?

Good Evening,

I am playing a sound like this:

public EventReference sound;

public void PlaySound()
{
       RuntimeManager.PlayOneShot(sound);
}

This works, but I need to stop it early sometimes.
What is the simplest way to stop playing this sound before it finishes?

Thanks!

Hi,

To stop an event instance before its natural end, you need a reference to it, which PlayOneShot() obviously doesn’t give you. However, PlayOneShot() is simply executing the following code,

var instance = RuntimeManager.CreateInstance(/* your EventReference here */);
instance.set3DAttributes(RuntimeUtils.To3DAttributes(position));
instance.start();
instance.release();

so I’d recommend copying that code and using it instead of calling PlayOneShot(), and storing the instance yourself so you can call instance.stop() with the appropriate stop mode when needed.

Hope that helps!

1 Like

Thanks for the reply again Louis, this definitely helps!

Though this does bring up two questions:

  1. For the line:
    "instance.set3DAttributes(RuntimeUtils.To3DAttributes(position));"
    This is if I want 3D sound, i.e, sound that plays relevant to the listener, right? If I want the sound to be uniform and the same volume, (2D?) can I just leave this out?

  2. Is this different than instance.stop()? Do I need to release every instance that I store in addition to stopping it?

Thanks again for taking the time!

No problem!

  1. This line is (put simply) for when you want to play a 3D sound, as it provides FMOD with the position and velocity of your sound so that it can be spatialized. If you want the sound to be unaffected by spatialization, and it doesn’t have a Spatializer effect on it, then you can ignore that line. If the event does have a Spatializer effect, then you’ll want to keep the line and set the position to match your listener.

  2. It is different from instance.stop() - instance.release() marks the event for release, meaning that it will be destroyed by FMOD when it has stopped playing. Best practice is to call release after starting any event instances when you no longer need to change their playback state, unless you want to play the same instance multiple times, or explicitly stop an instance and start it again later.

1 Like

Makes perfect sense! Thanks for the assistance!

1 Like