What Is The Correct Way To Play A New Song?

Good Evening All,

I created a method that stops playing the current song and then plays a new song. Though, I’m not certain if this is the best way to do it:

public void PlaySong(EventReference song)
    {
        // If a previous song is playing stop it.
        currentSong.stop(FMOD.Studio.STOP_MODE.IMMEDIATE);

        // Set the current song
        currentSong = RuntimeManager.CreateInstance(song);

        // Play the new song
        currentSong.start();
        
    }

Rather than always trying to stop a song, I wanted to do a check to see if currentSong is either null or currently playing. Though it looks like using if (currentSong != null) is not valid.

Is there a better way to first check if a song is playing, stop it if so, and then finally play a new song?

Thanks so much for taking the time! :blush:

Hi,

To check if FMOD variables are valid you can use currentSong.isValid() and to check if an instance is currently playing you can check its Playback State (FMOD Engine | Studio API Reference - Common)

currentSong.getPlaybackState(out FMOD.Studio.PLAYBACK_STATE playbackState);

Hope this helps

1 Like

Thank you very much for the response!

Just to clarify, isValid() returns false if its null and true if a song(EventReferenceAsset) is already playing, correct?

1 Like

Hi,

Apologies I missed linking the isValid() documentation here: FMOD Engine | Studio API Reference - Event Instance.
In summary, it will check the event instance has been instantiated not its current playback state. To check its current state use currentSong.getPlaybackState()

Hope this helps