Hello, I cannot get my unity script working as intended with PLAYBACK_STATE and I absolutely don’t know why.
I just spent my day trying to understand but I am still stuck.
Using Fmod 2.02 + Unity 2020.3.39f1.
I use my script to check if a sound is playing in order to not play it again once it is already playing but the PLAYBACK_STATE is always STOPPED. So it keeps restarting the sound each frame.
Here is the script I use :
To explain a bit, I call PlayOneShotAttachedToGO() at only one place in the update of another script.
using FMOD.Studio;
using FMODUnity;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioPlayer : MonoBehaviour
{
public EventReference soundEventReference;
EventInstance soundStateInstance;
void Start()
{
soundStateInstance = RuntimeManager.CreateInstance(soundEventReference);
}
public PLAYBACK_STATE playbackState;
public void PlayOneShotAttachedToGO()
{
if (soundStateInstance.isValid())
{
soundStateInstance.getPlaybackState(out playbackState);
if (playbackState == PLAYBACK_STATE.STOPPED)//keeps being STOPPED even if the sound is playing in game
RuntimeManager.PlayOneShotAttached(soundEventReference, gameObject);
}
}
public void PlayOneShotToTransformPosition()
{
if (soundStateInstance.isValid())
{
soundStateInstance.getPlaybackState(out playbackState);
if (playbackState == PLAYBACK_STATE.STOPPED)
RuntimeManager.PlayOneShot(soundEventReference, transform.position);
}
}
public void StopSound()
{
if (soundStateInstance.isValid())
{
soundStateInstance.getPlaybackState(out playbackState);
if (playbackState == PLAYBACK_STATE.PLAYING)
soundStateInstance.stop(FMOD.Studio.STOP_MODE.IMMEDIATE);
}
}
}
I cannot understand what is happening and why when I hear the sound played in-game in unity it keeps being STOPPED.
I hope someone will have a solution. Also, it’s no use to propose to get back on 2.01 because I was forced to upgrade to 2.02 because 3D spatialized sound was not working.