Skip to main content

AudioComponent

Class

Used to play audio in a Lens.

  • Available on all platforms
Platform details: Snapchat, SPECS, Camera Kit Web, Camera Kit Mobile fully supported.

Platform details

  • Snapchat — Fully supported.

  • SPECS — Fully supported.

  • Camera Kit Web — Fully supported.

  • Camera Kit Mobile — Fully supported.

Index

Remarks

You can assign an AudioTrackAsset to play through script, or through the Audio Component input in the Inspector panel of Lens Studio.

Example

//@input Component.AudioComponent audio
// Play once
script.audio.play(1);
// Play forever
script.audio.play(-1);
// If playing, stop
if(script.audio.isPlaying())
{
script.audio.stop(true);
}
// Set a callback for when the sound stops playing
script.audio.setOnFinish(function()
{
print("sound finished playing");
});

Extends

Properties

audioTrack

audioTrack: AudioTrackAsset

The audio asset currently assigned to play.

Default Value

null

duration

readonly duration: number

The length (in seconds) of the current sound assigned to play.


enabled

enabled: boolean

If disabled, the Component will stop enacting its behavior.

Default Value

true

Inherited from

Component.enabled


fadeInTime

fadeInTime: number

Length (in seconds) of a volume fade in applied to the beginning of sound playback.


fadeOutTime

fadeOutTime: number

Length (in seconds) of a volume fade out applied to the end of sound playback.


isBeingDestroyed

readonly isBeingDestroyed: boolean

Returns true if this Component is currently being destroyed. Becomes true as soon as destruction begins via Component#destroy or destruction of its owning SceneObject, before any OnDestroyEvent callbacks fire.

Since

Lens Scripting Version 364

Example

if (!script.isBeingDestroyed) {
// Component is still fully alive
}

Inherited from

Component.isBeingDestroyed


isEnabledInHierarchy

readonly isEnabledInHierarchy: boolean

Returns true if this Component, its SceneObject, and all of that SceneObjects parents are enabled.

Inherited from

Component.isEnabledInHierarchy


mixToSnap

mixToSnap: boolean

When true, records sound directly into the snap. This mode works only when all Audio Components in the scene are using mix to snap. In this case input from microphone will be ignored.

Since

Lens Scripting Version 147

Default Value

false

playbackMode

playbackMode: PlaybackMode

SPECS

How the audio should be played back. Useful in optimizing audio in Spectacles.

Default Value

Audio.PlaybackMode.LowPower
@snapOS

position

position: number

The current playback time in seconds


recordingVolume

recordingVolume: number

The volume of audio recorded to the snap, from 0 to 1.

Since

Lens Scripting Version 147

Default Value

1

sceneObject

readonly sceneObject: SceneObject

The SceneObject this component is on.

Inherited from

Component.sceneObject


spatialAudio

readonly spatialAudio: SpatialAudio

Spatial Audio settings.

Since

Lens Scripting Version 147


uniqueIdentifier

readonly uniqueIdentifier: string

Since

Lens Scripting Version 176

Inherited from

Component.uniqueIdentifier


volume

volume: number

A volume multiplier for any sounds played by this AudioComponent.

Default Value

1.0

Methods

destroy()

destroy(): void

Destroys the component.

Returns

void

Inherited from

Component.destroy


getSceneObject()

getSceneObject(): SceneObject

Returns the SceneObject the component is attached to.

Returns

SceneObject

Inherited from

Component.getSceneObject


getTransform()

getTransform(): Transform

Returns the Transform this component is attached to.

Returns

Transform

Inherited from

Component.getTransform


getTypeName()

static getTypeName(): TypeName<AudioComponent>

Returns the fully-qualified JavaScript type name for this class (for example, "Component.Camera"). This is a static accessor (no instance required) and returns the same value as the instance getTypeName().

Returns

TypeName<AudioComponent>

Overrides

Component.getTypeName


getTypeName()

getTypeName(): string

Returns the name of this object's type.

Returns

string

Inherited from

Component.getTypeName


isOfType()

isOfType(type): boolean

Returns true if the object matches or derives from the passed in type.

Parameters

ParameterType
typestring

Returns

boolean

Inherited from

Component.isOfType


isPaused()

isPaused(): boolean

Returns whether the sound is currently paused.

Returns

boolean


isPlaying()

isPlaying(): boolean

Returns whether the AudioComponent is currently playing sound.

Returns

boolean


isSame()

isSame(other): boolean

Returns true if this object is the same as other. Useful for checking if two references point to the same thing.

Parameters

ParameterType
otherScriptObject

Returns

boolean

Inherited from

Component.isSame


pause()

pause(): boolean

Pauses the sound.

Returns

boolean


play()

play(loops): void

Plays the current sound loops number of times. If loops is -1, the sound will repeat forever.

Parameters

ParameterType
loopsnumber

Returns

void


resume()

resume(): boolean

Resumes a paused sound.

Returns

boolean


setOnFinish()

setOnFinish(eventCallback): void

Sets the callback function to be called whenever this sound stops playing.

Parameters

ParameterType
eventCallback(audioComponent) => void

Returns

void


stop()

stop(fade): void

Stops the current sound if already playing.

Parameters

ParameterType
fadeboolean

Returns

void

See also

Playing Audio guide for more information.