Can unity Check if certain fmod event is playing?

Hey , im trying to create a sound effect script for jet pack jump in a game i work on.
I wondered if theres a way I could check if for example:

if Jetpack event is playing , return true

is this possible ?

If you are managing instances you can call EventInstance::getPlaybackState to get the current playback state of an instance, something like this would work:

bool IsPlaying(FMOD.Studio.EventInstance instance) {
	FMOD.Studio.PLAYBACK_STATE state;   
	instance.getPlaybackState(out state);
	return state != FMOD.Studio.PLAYBACK_STATE.STOPPED;
}

If you are using the StudioEventEmitter components you can reference your desired emitter in code and call StudioEventEmitter::IsPlaying.

hi alex ! thanks for the replay
can you show me an example of how it is done ?
im pretty new to coding

how do I chose which event does the “isPlaying” is checking ?

I suppose you are playing sounds using the StudioEventEmitter component:

grafik

Create a new script and declare a StudioEventEmitter at the start of the class:

[SerializeField]
private FMODUnity.StudioEventEmitter emitter;

Save the script and attach it to any GameObject you want. Drag the GameObject containing the StudioEventEmitter component to the emitter field in the inspector:

grafik

Now you just can call the isPlaying method to check if that specific emitter is playing or not:

void Update()
{
    if (emitter.IsPlaying())
    {
        Debug.Log("Emitter is playing");
    }
    else if (!emitter.IsPlaying())
    {
        Debug.Log("Emitter is not playing");
    }
}

If you are still new to coding, I would suggest to take a look at Unity’s learning resources, they are great to learn how Unity works and to get basic scripting knowledge.

im actually planing on making a script with Update that checks if the sound is playing
if he’s playing do nothing , else play the sound

that kinda solves me the whole jetpack thing
how should i reference the specific Jetpack event doing so in the script
with no emitter ?

ill ask this differently , where should i put the “isPlaying” method script so i could call it anywhere on every event instance ?

cause right now i put it on some AudioManager script i have ,
and called it from a different script , and it doesnt seem to work

how should i reference the specific Jetpack event doing so in the script
with no emitter ?

How are you actually playing your jetpack event? If by creating and playing an event instance in a script, then you can use the code snippet posted in my first answer in the same script.

where should i put the “isPlaying” method script so i could call it anywhere on every event instance ?

There are different ways to go about this, you could make the method static so you can access it everywhere:

public class FmodExtensions
{    
    public static bool IsPlaying(FMOD.Studio.EventInstance instance)
    {
        FMOD.Studio.PLAYBACK_STATE state;
        instance.getPlaybackState(out state);
        return state != FMOD.Studio.PLAYBACK_STATE.STOPPED;
    }
}

Then just call FmodExtensions.IsPlaying(yourInstance) from where you want.

I finally managed to make it work ! your commends was super helpful .
I put the IsPlaying() as a public static method in my main AudioPlayer script which controls several audio commands for the entire project ,
then called it as you sayed

This is the how it works in the end :

	void Update ()
	{
		if (jetpack.UsingJet) 
		{
            if (AudioPlayer.IsPlaying(JetpackEvent))
            {
			
	        }  
	        else
	        {
	            JetpackEvent.start();
	        }
        }
        else 
        {
        	JetpackEvent.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
        }
	}
1 Like