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:
-
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?
-
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!
Makes perfect sense! Thanks for the assistance!
1 Like
What if I want to do this but with PlayOneShotAttached()? What would be different? I can’t seem to get the sound to stay on the player.
For an equivalent to PlayOneShotAttached()
, instead of setting the event instance’s 3D attributes, you would call RuntimeManager.AttachInstanceToGameObject()
to get the event instance to follow the player’s transform.