Audio API
Audio API
createInnerAudioContext()
Creates an audio playback instance. Returns a new instance on each call.
Instance Properties (Read/Write):
| Member | Type | Description |
|---|---|---|
src | string | Audio resource URL |
startTime | number | Playback start position (seconds) |
autoplay | boolean | Whether to auto-play |
loop | boolean | Whether to loop |
volume | number | Volume (0~1) |
playbackRate | number | Playback speed (0.5~2.0) |
Instance Properties (Read-only):
| Member | Type | Description |
|---|---|---|
duration | number | Total duration (seconds) |
currentTime | number | Current playback position (seconds) |
paused | boolean | Paused state |
buffered | number | Buffered time (seconds) |
Instance Methods:
| Member | Type | Description |
|---|---|---|
play() | void | Play |
pause() | void | Pause |
stop() | void | Stop (resets position to 0) |
seek(position) | void | Seek to a specific position (in seconds) |
destroy() | void | Destroy instance and release resources |
Instance Events:
| Member | Type | Description |
|---|---|---|
onCanplay(cb) / offCanplay(cb?) | void | Ready to play |
onPlay(cb) / offPlay(cb?) | void | Playback started |
onPause(cb) / offPause(cb?) | void | Paused |
onStop(cb) / offStop(cb?) | void | Stopped |
onEnded(cb) / offEnded(cb?) | void | Playback ended |
onTimeUpdate(cb) / offTimeUpdate(cb?) | void | Playback time updated |
onError(cb) / offError(cb?) | void | Error occurred |
onWaiting(cb) / offWaiting(cb?) | void | Buffering |
onSeeking(cb) / offSeeking(cb?) | void | Seeking started |
onSeeked(cb) / offSeeked(cb?) | void | Seeking completed |
const audio = TudadaSDK.createInnerAudioContext();
audio.src = './bgm.mp3';
audio.loop = true;
audio.volume = 0.8;
audio.play();
const bgm = TudadaSDK.createInnerAudioContext();
bgm.src = './bgm.mp3';
bgm.loop = true;
bgm.volume = 0.5;
bgm.onCanplay(() => {
console.log('Ready to play');
bgm.play();
});
bgm.onError((err) => {
console.error('Audio error:', err.errMsg);
});
bgm.onEnded(() => {
console.log('Playback complete');
});
// Always call destroy() after use to clean up
bgm.destroy();