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++;
}
相关推荐
b1305381004925 分钟前
HarmonyOS应用《玄象》开发实战:命盘排布 Canvas:四柱干支 + 六十甲子纳音表的同步绘制
harmonyos·鸿蒙
木木子2228 分钟前
# 鸿蒙ArkTS实战:折扣计算器 — 快速百分比选择与省钱明细展示
华为·harmonyos
爱写代码的阿森35 分钟前
鸿蒙三方库 | harmony-utils之ArrayUtil集合过滤排序与分块详解
华为·harmonyos·鸿蒙·huawei
不言鹅喻37 分钟前
HarmonyOS ArkTS 实战:实现一个掷骰子模拟器
华为·harmonyos
LEO111102 小时前
HarmonyOS应用《玄象》开发实战:RenderingContextSettings(true) 抗锯齿对 Canvas 渲染的影响
harmonyos·鸿蒙
爱写代码的阿森2 小时前
鸿蒙三方库 | harmony-utils之ArrayUtil集合操作与去重详解
华为·harmonyos·鸿蒙·huawei
qizayaoshuap3 小时前
# 评分器组件开发实战:HarmonyOS ArkTS 五星评分交互应用解析
华为·交互·harmonyos
b130538100493 小时前
HarmonyOS应用《玄象》开发实战:AiPhotoFengshuiPage AI 拍照风水:@ohos.multimedia.camera 相机调用
harmonyos·鸿蒙
爱写代码的阿森3 小时前
鸿蒙三方库 | harmony-utils之NumberUtil数值精确计算详解
华为·harmonyos·鸿蒙·huawei
b130538100495 小时前
HarmonyOS应用《玄象》开发实战:HomePage 顶部标题栏的 Row + Blank 三段式布局
harmonyos·鸿蒙