Pausing Music and Resuming from Same Location?

Hi everyone!

I have a question regarding music integration. I’m hoping someone can help.

In this game I’m working on we have music that is triggered in the main level, but when a player enters a building that music stops until the player exits the building.

Upon exiting the building the music comes back on but always starts at the beginning of the piece.

Is it possible in F-MOD to set it up so that when exiting a building the music resumes from where it left off before the player entered the building?

Thanks,
Kristi

It sounds like you might be calling Play on the sound to resume it after the pause.
Calling play on an event will start it from the beginning, you need to use setPaused(false) to resume a paused event.

https://fmod.com/resources/documentation-api?page=content/generated/FMOD_Studio_EventInstance_SetPaused.html#/

Thank you Cameron. Is this something on the code side? So I need to set my event up in F-Mod Studio any differently? I just have an event set to looping, nothing fancy.

Just the code, you don’t need to change the event at all.

Use EventInstance.setPaused(true) to pause.
And EventInstance.setPaused(false) to resume.

1 Like

Awesome thanks!

I’m trying to do the same thing, and followed the suggestion laid out here, but I’m getting a console error:

‘EventInstance’ does not contain a definition for ‘EventInstance’ and no accessible extension method ‘EventInstance’ accepting a first argument of type ‘EventInstance’ could be found (are you missing a using directive or an assembly reference?

Here’s my script:

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

public class MusicManager : MonoBehaviour
{
    public FMOD.Studio.EventInstance levelMusic;
    public GameObject pauseMenuOB;
    private bool pauseMenuOn;

    void Awake()
    {
        PauseMenu pauseMenu = pauseMenuOB.GetComponent<PauseMenu>();
        pauseMenuOn = pauseMenu.pauseMenuOn;

        levelMusic = FMODUnity.RuntimeManager.CreateInstance("event:/Music/Chapter1/1_1_Lobby");
        levelMusic.start();
    }

    void Update()
    {
        PauseMenu pauseMenu = pauseMenuOB.GetComponent<PauseMenu>();
        pauseMenuOn = pauseMenu.pauseMenuOn;

        if (pauseMenuOn) levelMusic.EventInstance.setPaused(true);

        else if (!pauseMenuOn) levelMusic.EventInstance.setPaused(false);
    }
}

Not sure what I’m doing wrong here, any ideas?

Hi,

Your levelMusic is the FMOD Engine | Studio API Reference - Event Instance created by Unity Integration | Scripting API Reference - RuntimeManager::CreateInstance.

The solution should be removing the EventInstance from both the setPaused() function calls.

Hope this helps!

Okay, so I modified the Update to this:

void Update()
    {
        PauseMenu pauseMenu = pauseMenuOB.GetComponent<PauseMenu>();
        pauseMenuOn = pauseMenu.pauseMenuOn;

        if (pauseMenuOn) EventInstance.setPaused(true);

        else if (!pauseMenuOn) EventInstance.setPaused(false);
    }

But now, I’m getting an Object Reference error: “An object reference is required for the non-static field, method, or property ‘EventInstance.setPaused(bool)’”

Hi,

Apologies, I wasn’t very clear. It would be like this:

void Update()
{
    PauseMenu pauseMenu = pauseMenuOB.GetComponent<PauseMenu>();
    pauseMenuOn = pauseMenu.pauseMenuOn;

    if (pauseMenuOn) levelMusic.setPaused(true);

    else if (!pauseMenuOn) levelMusic.setPaused(false);
}

Hope this helps!

Yep, that did the trick! Thank you!

1 Like