Skip to main content

AudioComponent

Audio component.

TypeNameInterface Description
VariablesloopCount: number

Function: The number of times the audio should loop.

Variablesvolume: number

Function: The volume level of the audio. Default is 100. Accepts integer or floating-point values in the range [0, 100]. Values above 100 are clamped to 100, and values below 0 are clamped to 0.

Variablesduration: number

Function: Gets the duration of the audio in seconds.

VariablesisFinished: boolean

Function: Whether the audio has finished playing.

Functionsconstructor()

Functionspause(): void

Function: Pause audio playback.

Functionsplay(): void

Function: Start audio playback.

Functionsresume(): void

Function: Resume audio playback.

Functionsstop(): void

Function: Stop audio playback.

Examples

constructor()

let obj = new APJS.AudioComponent();

Use Case

Example 1 — Audio manager with BGM loop + multiple SFX channels — demonstrates the BGM+SFX pattern with separate Audio Player objects

@component()
export class MultiSfxManager extends APJS.BasicScriptComponent {
@serializeProperty
bgmPlayer!: APJS.SceneObject;
@serializeProperty
clickPlayer!: APJS.SceneObject;
@serializeProperty
successPlayer!: APJS.SceneObject;
@serializeProperty
failPlayer!: APJS.SceneObject;

private bgm!: APJS.AudioComponent;
private clickSfx!: APJS.AudioComponent;
private successSfx!: APJS.AudioComponent;
private failSfx!: APJS.AudioComponent;
private initialized = false;

onUpdate(dt: number): void {
if (!this.initialized && this.bgmPlayer) {
this.bgm = this.bgmPlayer.getComponent("AudioComponent") as APJS.AudioComponent;
this.clickSfx = this.clickPlayer.getComponent("AudioComponent") as APJS.AudioComponent;
this.successSfx = this.successPlayer.getComponent("AudioComponent") as APJS.AudioComponent;
this.failSfx = this.failPlayer.getComponent("AudioComponent") as APJS.AudioComponent;
if (!this.bgm || !this.clickSfx) return;

// Start BGM (playMode set to Infinity via DSL)
this.bgm.volume = 40;
this.bgm.play();
this.initialized = true;
}
}

playClick(): void {
this.clickSfx.stop();
this.clickSfx.loopCount = 1;
this.clickSfx.volume = 100;
this.clickSfx.play();
}

playSuccess(): void {
this.successSfx.stop();
this.successSfx.loopCount = 1;
this.successSfx.volume = 100;
this.successSfx.play();
}

playFail(): void {
this.failSfx.stop();
this.failSfx.loopCount = 1;
this.failSfx.volume = 100;
this.failSfx.play();
}
}

Example 2 — Play a short sound effect on each screen tap — stop before play to allow rapid repeated taps

@component()
export class TapSoundEffect extends APJS.BasicScriptComponent {
@serializeProperty
sfxPlayer!: APJS.SceneObject;

private sfx!: APJS.AudioComponent;
private initialized = false;
private touchCallback!: (event: APJS.IEvent) => void;

onUpdate(dt: number): void {
if (!this.initialized && this.sfxPlayer) {
this.sfx = this.sfxPlayer.getComponent("AudioComponent") as APJS.AudioComponent;
if (!this.sfx) return;

this.touchCallback = (event: APJS.IEvent) => {
const touch = event.args[0] as APJS.TouchData;
if (touch.phase !== APJS.TouchPhase.Began) return;
// Stop first to reset if already playing (allows rapid taps)
this.sfx.stop();
this.sfx.loopCount = 1;
this.sfx.play();
};
APJS.EventManager.getGlobalEmitter().on(APJS.EventType.Touch, this.touchCallback, this);
this.initialized = true;
}
}

onDestroy(): void {
if (this.touchCallback) {
APJS.EventManager.getGlobalEmitter().off(APJS.EventType.Touch, this.touchCallback, this);
}
}
}
Copyright © 2026 TikTok. All rights reserved.
About TikTokHelp CenterCareersContactLegalTerms of ServicePrivacy PolicyCookies