FMOD’s Playback States allow us to get the playback state of an Event. Is the Event currently playing or in a stopped state? Is the Event pausing at a sustain point? The FMOD Unreal Integration already provides us with a isPlaying node, but if we want or need finer control over the playback states of instances, we might like to add additional nodes to our Blueprint tools.
Checking the Playback State of FMOD Event Instances
We can modify the FMOD Integration source code to add a Blueprint node that will help us to retrieve the Playback State of Event Instances.
In the FMODBlueprintStatics header file, declare the enumerator EFMOD_STUDIO_PLAYBACK_STATE
:
UENUM(BlueprintType)
enum class EFMOD_STUDIO_PLAYBACK_STATE : uint8
{
PLAYBACK_PLAYING UMETA(DisplayName = "Playing"),
PLAYBACK_SUSTAINING UMETA(DisplayName = "Sustaining"),
PLAYBACK_STOPPED UMETA(DisplayName = "Stopped"),
PLAYBACK_STARTING UMETA(DisplayName = "Starting"),
PLAYBACK_STOPPING UMETA(DisplayName = "Stopping")
};
Also declare the GetPlaybackState
function:
UFUNCTION(BlueprintCallable, Category = "Audio|FMOD", meta = (UnsafeDuringActorConstruction = "true"))
static EFMOD_STUDIO_PLAYBACK_STATE GetPlaybackState(FFMODEventInstance EventInstance);
In FMODBlueprintStatics.cpp implement the GetPlaybackState function:
EFMOD_STUDIO_PLAYBACK_STATE UFMODBlueprintStatics::GetPlaybackState(FFMODEventInstance EventInstance)
{
FMOD_STUDIO_PLAYBACK_STATE pS;
if (EventInstance.Instance)
{
FMOD_RESULT Result = EventInstance.Instance->getPlaybackState(&pS);
if (Result != FMOD_OK)
{
UE_LOG(LogFMOD, Warning, TEXT("Failed to get PlaybackState of Instance"));
}
}
return (EFMOD_STUDIO_PLAYBACK_STATE)pS;
}
We simply pass a FFMODEventInstance
as an argument and return the playback state of the Event Instance. Build the project and launch the Unreal Editor. We can now use the new GetPlaybackState Blueprint node to accurately check the playback state of an Event Instance:
Checking the Playback State of FMOD Audio Components
The FMOD Audio Component already has a IsPlaying node that we can use to check if an Event is actually playing. If we want finer control over the playback states when using Audio Components we can also create a new node that replicates the same behavior as above. Let’s declare another function in the FMODBlueprintStatics header file:
UFUNCTION(BlueprintCallable, Category = "Audio|FMOD", meta = (UnsafeDuringActorConstruction = "true"))
static EFMOD_STUDIO_PLAYBACK_STATE GetPlaybackStateAC(class UFMODAudioComponent *AudioComponent);
And implement that function in the .cpp file:
EFMOD_STUDIO_PLAYBACK_STATE UFMODBlueprintStatics::GetPlaybackStateAC(class UFMODAudioComponent* AudioComponent)
{
FMOD_STUDIO_PLAYBACK_STATE pS;
if (AudioComponent != nullptr)
{
if (AudioComponent->StudioInstance)
{
FMOD_RESULT Result = AudioComponent->StudioInstance->getPlaybackState(&pS);
if (Result != FMOD_OK)
{
UE_LOG(LogFMOD, Warning, TEXT("Failed to get PlaybackState of Audio Component"));
}
}
}
return (EFMOD_STUDIO_PLAYBACK_STATE)pS;
}
When working with Audio Components in Blueprints, we can now use the GetPlaybackStateAC node to check for the playback state of an Audio Component:
Usually the provided IsPlaying node should be sufficient for most use cases. This method of checking playback states might be still useful in advanced game behaviors.