Hello everyone, in my game, I’m encountering an issue with Unity and FMOD in WebGL builds. The problem is similar to what occurs in the bouncing ball FMOD demo: FMOD HTML5 Demo.
When I load the banks, start the scene, and then switch tabs, the game audio begins to stutter. I hypothesize this only happens to streamed audios, I wonder what is the common practice in these scenarios, if continue playing the music or pause/resume all events, also would appreciate any advice on which API functions to study for that matter.
Thank you in advance for any advice!
Hi,
An option when the game enters the background is to suspend the mixer: FMOD Engine | Core API Reference - System::mixerSuspend. This will maintain all internal states while reducing CPU usage to 0%. Mainly useful on mobile platforms. When the game resumes focus resume the mixer: FMOD Engine | Core API Reference - System::mixerResume.
However, we have to call JavaScript functions from Unity C# scripts:
- First set up a Java Plugin: Unit Docs | Set up your JavaScript plug-in
- As we use the
MyGameInstance
object in the .jslib
file we need to initialize it in our index.html
file: Unity Docs | Call JavaScript functions from Unity C# scripts - Call JavaScript functions from global scope.
Here is how I am pausing/resuming the mixer in my script:
public class TriggerPause : MonoBehaviour
{
[DllImport("__Internal")]
private static extern void AddListener();
private bool paused = false;
void Start()
{
AddListener();
}
void suspend()
{
if (paused)
{
FMODUnity.RuntimeManager.CoreSystem.mixerResume();
paused = false;
}
else
{
FMODUnity.RuntimeManager.CoreSystem.mixerSuspend();
paused = true;
}
}
}
Hope this helps!