Playing FMOD Events in Unreal Engine 4

In this tutorial we will learn how to play FMOD Events in Unreal Engine 4 by using the Blueprint nodes provided by the FMOD Integration plugin and by writing simple C++ code.

Playing Events with the FMOD Ambient Sound Actor

The FMOD Ambient Sound is an Actor that holds an FMODAudio component. Locate the Ambient Sound from the Modes tab on the left and drag it into the level:

FMOD Ambient Sound in Unreal Engine 4
FMOD Ambient Sound in Unreal Engine 4

We can choose the FMOD Event in the Details tab and select if we want the Event to automatically play when the game starts by checking the Auto Activate checkbox:

FMOD Ambient Sound details
FMOD Ambient Sound details

If you want to manually activate the sound, convert the Actor into a Blueprint Class and reference the FMODAudioComponent, then use the Play node to play the sound:

FMODAudioComponent in Unreal Engine 4
FMODAudioComponent in Unreal Engine 4

Playing FMOD Events by using Blueprints

The FMOD Integration provides us with different easy-to-use nodes that we can use to play and manage FMOD Events. Let’s take a look at the various nodes.

Playing a 2D FMOD Event

To play a 2D FMOD Event in Unreal Engine 4 we can use the Play Event 2D node:

FMOD Play Event 2D node in Unreal Engine 4
FMOD Play Event 2D node in Unreal Engine 4

We connect the Event BeginPlay node to the Play Event 2D node, select a 2D Event in the FMODEvent Object Reference field and start the level. The sound will play at the start of the level.

If the Auto Play bool is active, the Event will automatically start and release the instance from memory after it finishes playing. Since the node returns an Event Instance, we can also think about deciding ourselves when to release the instance and we can do this manually by using the Event Instance Play and Event Instance Release node:

FMOD Event Instance Play and Release Node
FMOD Event Instance Play and Release Node

Playing a Loop

In addition to the release node, we have access to the Event Instance Stop node that can be use to stop a loop for example:

FMOD Event Instance Stop Node
FMOD Event Instance Stop Node

The Release checkbox in that node will automatically release the instance from memory. Unchecking the Release checkbox means we have to release the instance manually by using the Event Instance Release node.

Playing 3D FMOD Events

If we are using the Play Event 2D node and select a 3D Event from the event list, the sound will be spatialized and played in 3D space at the Actor’s location. The Play Event 2D node uses the Play Event at Location node under the hood. This also means that the event doesn’t move with Actor. If we still want to update the 3D coordinates of the Event Instance, we can use the Event Instance Set Transform node:

FMOD Event Instance Set Transform node
FMOD Event Instance Set Transform node

In this Blueprint we simply get the Event Instance from the Play Event 2D node, check if the Event Instance is valid and update the 3D Attributes of the Event Instance using the GetActorLocation node.

PlayEventAttached

We can also use the Play Event Attached node to attach an FMOD Event to a component. The 3D Attributes of the Event will automatically update:

FMOD Play Event Attached node
FMOD Play Event Attached node

Here we are attaching the Event to the Root Component and playing the Event when pressing the 1 key. This node returns a FMODAudio Component, therefore we use the Stop and Release nodes to stop and release the instance from memory.

Playing FMOD Events by writing C++ code

Playing FMOD Events by writing C++ code is also very easy. We can interface with the same Blueprint nodes or directly gain access to the FMOD API.

Preparation for playing FMOD Events with C++

I will use the ThirdPersonExample that can be created when starting Unreal Engine for all C++ examples. We will directly work inside Character.h and Character.cpp to make things very easy.

In Unreal’s project preferences, switch to the Input settings and create two new Action Mappings:

Unreal Engine 4 Action Mappings example
Unreal Engine 4 Action Mappings example

We will play a sound when pressing the 1 key on our keyboard and stop the sound by pressing the 2 key.

In the ThirdPersonCharacter header file, include FMODBlueprintStatics.h and declare a UFMODEvent variable with the UPROPERTY macro:

UPROPERTY(EditAnywhere, Category = "FMOD")
class UFMODEvent* Event;

The Property Specifier EditAnywhere allows us to see and select the Event in the Details tab of our ThirdPersonCharacter:

FMOD Event in the Details tab
FMOD Event in the Details tab

Declare the FFMODEventInstance variable, which is a wrapper for FMOD’s Event Instances:

FFMODEventInstance InstanceWraper;

In the same header file, declare the functions PlaySound and StopSound:

void PlaySound();
void StopSound();

Before proceeding, let’s also include FMODBlueprintStatics.h and bind the two Action Mappings in the SetupPlayerInputComponent function of your Character.cpp file:

// FMOD
PlayerInputComponent->BindAction("PlaySound", IE_Pressed, this, &ATutorialCharacter::PlaySound);
PlayerInputComponent->BindAction("StopSound", IE_Pressed, this, &ATutorialCharacter::StopSound);

Playing and stopping an FMOD Event using C++

We can now finally implement the two functions we declared earlier. In the Character.cpp file we will play an Event by calling UFMODBlueprintStatics::PlayEvent2D. PlayEvent2D will return an FFMODEventInstance. So we will assign the return value of PlayEvent2D to the InstanceWrapper:

void ATutorialCharacter::PlaySound()
{
InstanceWrapper = UFMODBlueprintStatics::PlayEvent2D(GetWorld(), Event, true);
}

By having access to the FFMODEventInstance, we can get the actual Event Instance and stop and release the instance by using FMOD’s Studio API:

void ATutorialCharacter::StopSound()
{
	InstanceWrapper.Instance->stop(FMOD_STUDIO_STOP_ALLOWFADEOUT);
	InstanceWrapper.Instance->release();
}

Compile, select your desired FMOD Event in the Character’s Details tab, start the level and press 1 to play and 2 to stop the Event.

Updating the 3D Attributes of an FMOD Event Instance by using C++

If you played a 3D Event, you probably noticed that the sound doesn’t travel with the Actor. As we mentioned at the beginning of this tutorial, PlayEvent2D will call PlayEventAtLocation and therefore the location of the sound will not be updated. We can solve this by calling EventInstanceSetTransform every frame. Since our Tutorial Character doesn’t have a Tick function, let’s create this first:

virtual void Tick(float DeltaTime) override;

Add this code line to the Character constructor:

PrimaryActorTick.bCanEverTick = true;

Now we can implement the Tick function and check if the Event Instance is valid, if it is we update the 3D Attributes of the instance with the Actor’s transform:

void ATutorialCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	if (UFMODBlueprintStatics::EventInstanceIsValid(InstanceWrapper)) 
	{
		UFMODBlueprintStatics::EventInstanceSetTransform(InstanceWrapper, GetActorTransform());
	}
}

PlayEventAttached

Alternatively, we can play and attach the instance to a component by using PlayEventAttached:

void ATutorialCharacter::PlaySound()
{
	AudioComponent = UFMODBlueprintStatics::PlayEventAttached(Event, RootComponent, NAME_None, GetActorLocation(), EAttachLocation::KeepWorldPosition, true, true, true);
}

void ATutorialCharacter::StopSound()
{
	AudioComponent->Stop();
        AudioComponent->Release();
}

PlayEventAttached returns a UFMODAudioComponent, so make sure to declare it in your header file if you want to be able to access and stop the sound:

UPROPERTY(EditAnywhere, Category = "FMOD")
class UFMODAudioComponent* AudioComponent;

Congratulations, you now know how to play FMOD Events in Unreal Engine 4. In the next tutorial we will control Event Parameters by using Blueprints and writing C++ code.

↑ To the Top