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++;
}
相关推荐
2501_919749031 天前
华为鸿蒙管理密码APP—小羊密码
华为·harmonyos·鸿蒙
ITUnicorn1 天前
【HarmonyOS】时间管理类APP:做成“自适应“
harmonyos
笔触狂放1 天前
第2章 ArkTS(上)
华为·harmonyos·鸿蒙
新元代码1 天前
探秘鸿蒙南向开发:Hi3861 架构、编译与实战全解析
华为·架构·harmonyos
笔触狂放1 天前
第1章 初识鸿蒙
华为·harmonyos
小雨青年1 天前
【HarmonyOS 7 沉浸光感深度实战】 06 复杂背景下的自动反色与可读性处理
华为·harmonyos
水深火乐1 天前
HmarkX(码笺)的本地数据库模块重构实战
harmonyos
大锅盖11 天前
Map 搜索没有返回可信候选时,派单页面该怎么继续工作
华为·harmonyos
HarmonyOS_SDK1 天前
HarmonyOS SDK API诊断 Skill,一键定位功能故障问题
harmonyos
大锅盖11 天前
Canvas 签名板里的 antialias 开关,怎样确保它真正触发重绘
华为·harmonyos