typescript
复制代码
private player?: media.SoundPool;
async initAndLoad() {
await this.initSoundPool();
await this.loadSounds();
this.ready = true;
}
async release() {
await this.player?.release();
this.ready = false;
}
async initSoundPool() {
this.player = await media.createSoundPool(1, {
usage: audio.StreamUsage.STREAM_USAGE_GAME,
rendererFlags: 1
},)
}
async loadSounds() {
this.soundIds = [];
for (let i = 0; i < this.audioOptions.length; i++) {
let audio: AudioOption = this.audioOptions[i];
let id = await this.loadSoundId(audio.src);
this.soundIds.push(id);
}
}
async loadSoundId(rawFile: string): Promise<number> {
let res = await getContext().resourceManager.getRawFd(rawFile);
return new Promise<number>((resolve, reject) => {
this.player!.load(res.fd, res.offset, res.length, (error, id: number) => {
if (!error) {
resolve(id);
} else {
reject(error);
}
})
});
}
tick(): void {
if (!this.ready) {
return;
}
this.player?.play(this.soundIds[this.activeAudioIndex]);
this.counter++;
}