Harmony SoundPool 的初始化 加载资源 播放

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++;
}
相关推荐
半路下车3 小时前
【Harmony OS 5】HarmonyOS应用测试指南
设计模式·harmonyos
移动端开发者3 小时前
鸿蒙Next自定义双滑块滑动条实现方案
harmonyos
SunFlower013 小时前
Harmony-websocket(一)
harmonyos
颜颜yan_3 小时前
深入解析HarmonyOS5 UIAbility组件:从核心架构到实战应用
架构·harmonyos·鸿蒙·鸿蒙系统
程序员小刘3 小时前
【HarmonyOS 5】教育开发实践详解以及详细代码案例
华为·wpf·harmonyos
半路下车3 小时前
【Harmony OS 5】HarmonyOS应用测试进阶
测试·harmonyos
Georgewu15 小时前
【HarmonyOS 5】桌面快捷方式功能实现详解
harmonyos
娅娅梨17 小时前
HarmonyOS-ArkUI 自定义弹窗
华为·harmonyos·arkts·arkui