Skip to content

Audio

Play sounds, control audio sources, and manage video playback through configurable operations.

Instead of writing playback scripts, you compose audio and video behaviors from modular operations in the Inspector. Need to play a clip, adjust volume, and enable looping? Add those operations to a single command's list. Need to wait for a sound to finish before the next step? Use the play audio operation. Everything works with both AudioSource and VideoPlayer components.

Assembly: Jungle.Audio
Dependencies: Jungle.Core, Jungle.Objects
Namespace: Jungle.Audio, Jungle.Audio.GameDev


AudioSource Command

The AudioSource Command lets you batch multiple audio operations into one process. Pick an AudioSource target, then add the operations you need - play, set clip, adjust volume, and more - all in one Inspector entry.

Technically, this is AudioSourceCommand, a targeted command (TargetedCommand<AudioSource, IAudioOperation>) that executes a configurable list of audio operations in parallel:

[SerializeReference]
[JungleClassSelection]
private IProcess audioCommand = new AudioSourceCommand();
Inspector
Process Component? ⇅ ⋮
Process
Start & Stop
ProcessAudioSource Command
Target1Speaker (Audio Source)◎
Operations0+
No operations
A new AudioSource Command: point it at an Audio Source, then click + to add operations.

Available Operations

Each operation is one entry in the command's Operations list (they implement IAudioOperation):

Operation Description
Play Play, Stop, Pause, or UnPause the source. Use the PlaybackMode enum to pick the operation.
Set Clip Assigns an AudioClip to the source. Uses an IAudioClipValue provider so the clip can come from a direct reference, a ScriptableObject asset, or any other value source.
Set Volume Sets audioSource.volume via a float pipeline (target value + modifier chain).
Set Pitch Sets audioSource.pitch via a float pipeline. Values above 1 speed up playback; below 1 slow it down.
Set Spatial Blend Sets audioSource.spatialBlend via a float pipeline. 0 = fully 2D, 1 = fully 3D.
Set Loop Enable, Disable, or Toggle looping.
Set Mute Mute, Unmute, or Toggle mute. Muting preserves playback position unlike Stop.

Instant vs Timed Operations

Operations fall into two categories:

  • Instant operations extend InstantAudioOperation and complete immediately (Play, Set Clip, Set Loop, Set Mute). They override Execute(AudioSource) and the base handles the IAudioOperation plumbing.
  • Timed operations implement IAudioOperation directly and self-tick until finished. PlayAudioOperation plays a clip and completes when it ends. AudioFloatPipelineOperation drives a float property (volume, pitch, or spatial blend) over time using the pipeline pattern with targets, modifiers, and a completion check.

The command runs all operations in parallel and completes when every operation has finished.

Pipeline Operations (Volume, Pitch, Spatial Blend Over Time)

AudioFloatPipelineOperation can drive volume, pitch, or spatial blend over time using the pipeline pattern: target + modifier chain + completion check. Select which AudioFloatProperty to drive, configure an IFloatTarget for the desired curve, and add optional IFloatModifier entries for smoothing or clamping.

For instant one-shot changes, SetVolume, SetPitch, and SetSpatialBlend extend InstantAudioOperation and write the value immediately.


Play Audio Operation

PlayAudioOperation is a timed audio operation that plays a clip via PlayOneShot and stays alive until the clip finishes. Use it inside an AudioSourceCommand when the next step should wait for playback to end.

Property Behavior
IsComplete Returns true once the clip has finished playing (or immediately if no clip is set).
Begin() Plays the clip via PlayOneShot and schedules completion after clip.length.
Complete() Stops the source immediately and cancels the pending timer.
Pause() Pauses the AudioSource.
Unpause() Resumes the AudioSource.

Video Player Command

VideoPlayerCommand works the same way as AudioSourceCommand but targets a Unity VideoPlayer. Operations are composed from IVideoOperation implementations in a list. It follows the TargetedCommand<VideoPlayer, IVideoOperation> pattern.

[SerializeReference]
[JungleClassSelection]
private IProcess videoCommand = new VideoPlayerCommand();

Available Video Operations

Operation Description
Play Starts or resumes video playback.
Pause Pauses playback.
Stop Stops playback and resets to the beginning.
Set Clip Assigns a VideoClip to the player.
Set Loop Enables or disables video looping.
Set Playback Speed Sets the speed multiplier (1.0 = normal).

Value Providers

Jungle.Audio defines value interfaces and providers for audio-related types, following the standard Jungle values pattern.

AudioSource Values

An AudioSource is referenced with SmartAudioSource, the generated smart-component value for AudioSource. One field covers every way of getting hold of one: a direct reference, a lookup on another object, or a live value resolved at runtime. See Smart Values for the modes.

Type Description
SmartAudioSource The AudioSource field used by every Jungle audio command and operation.

There is no separate AudioSource value asset. To share one AudioSource between components, put it on a GameObject Value asset (or a DataStorage key) and resolve the component from there.

AudioClip Values

Type Description
IAudioClipValue Interface for providing an AudioClip reference.
AudioClipValue Inline reference stored directly on the component. (default)

Video Values

Type Description
SmartVideoPlayer The VideoPlayer field used by the video commands.
IVideoClipValue Interface for providing a VideoClip reference.
VideoClipValue Inline reference stored directly on the component.

Usage Examples

Compose multiple audio operations

Use AudioSourceCommand to set volume, assign a clip, and play — all in one command:

[SerializeReference]
[JungleClassSelection]
private IProcess setupAndPlay = new AudioSourceCommand(
    new SetVolume(),
    new SetClip(),
    new Play()
);

In the Inspector, each operation is one entry in the Operations list. Add only what you need.

ProcessAudioSource Command
TargetSpeaker (Audio Source)◎
Operations3+
Operation 1Set Volume
Volume0.8
Operation 2Set Clip
ClipDoor Creak
Operation 3Play
ModePlay
Set the volume, swap the clip, then play: three operations that all run together.

Wait for a sound to finish before continuing

Add a PlayAudioOperation to an AudioSourceCommand. The command stays alive until the clip finishes:

[SerializeReference]
[JungleClassSelection]
private IProcess playAndWait = new AudioSourceCommand(
    new PlayAudioOperation()
);

Fade volume over time

Use AudioFloatPipelineOperation inside an AudioSourceCommand to drive volume with a lerped target and optional modifiers:

[SerializeReference]
[JungleClassSelection]
private IProcess fadeOut = new AudioSourceCommand(
    new AudioFloatPipelineOperation()
);

Configure the AudioFloatProperty to Volume, set the target to a lerped value, and add a clamp modifier if needed.

Share an AudioSource across multiple components

Point every command's SmartAudioSource at the same GameObject rather than at a shared asset. A SmartAudioSource in Dynamic mode resolves its AudioSource at runtime from whatever the field's source hands it, so a GameObject Value asset or a DataStorage key is enough to decouple the reference from any single component:

[SerializeField]
private SmartAudioSource source;