PlayAudioAction
An action which plays an audio resource on the given target entity.
Declaration
struct PlayAudioActionOverview
Use this action to initiate the playback of audio in a data-driven way. For example, this action can play an animation group which contains a nested action, which plays audio with specific playback properties.
This action plays the AudioResource from the AudioLibraryComponent on the targetEntity. Add all audio resources to the audio library component that this action will play, ensuring the name of the audio matches the audioResourceName being supplied.
useControlledPlayback is used to determine the playback behavior of the audio this action is playing. Set this to true to control the playback of the audio. Calling methods such as pause() and resume() on the animation playback controller generated from this action will give you control of the audio being played. When the action ends, the audio playback also ends. Set this to false to ensure the audio being played runs independently of the action. This would behave as a one shot audio.
The example below creates an animation which will play an audio resource after five seconds.
// Create an action which plays a specified audio resource.
//
// The audio resource exists within the target entity's
// animation library component.
//
// Control playback is set to false, audio will
// play independently of the animation.
let snapAudioAction = PlayAudioAction(audioResourceName: "snap",
useControlledPlayback: false)
// Creates an animation from the action, with a five second delay.
let snapAudioAnimation = try AnimationResource
.makeActionAnimation(for: snapAudioAction,
delay: 5.0)
// Play the animation which will play the audio after five seconds.
entity.playAnimation(snapAudioAnimation)