AudioComponent
Audio component.
| Type | Name | Interface Description |
|---|---|---|
| Variables | loopCount: number | • Function: The number of times the audio should loop. |
| Variables | volume: number | • Function: The volume level of the audio. Default is |
| Variables | duration: number | • Function: Gets the duration of the audio in seconds. |
| Variables | isFinished: boolean | • Function: Whether the audio has finished playing. |
| Functions | constructor() | |
| Functions | pause(): void | • Function: Pause audio playback. |
| Functions | play(): void | • Function: Start audio playback. |
| Functions | resume(): void | • Function: Resume audio playback. |
| Functions | stop(): 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);
}
}
}