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++;
}
相关推荐
MarkHD4 小时前
第十八天 WebView深度优化指南
华为·harmonyos
塞尔维亚大汉5 小时前
OpenHarmony(鸿蒙南向)——平台驱动开发【MIPI CSI】
harmonyos·领域驱动设计
别说我什么都不会5 小时前
鸿蒙轻内核M核源码分析系列十五 CPU使用率CPUP
操作系统·harmonyos
塞尔维亚大汉7 小时前
OpenHarmony(鸿蒙南向)——平台驱动开发【I3C】
harmonyos·领域驱动设计
今阳9 小时前
鸿蒙开发笔记-6-装饰器之@Require装饰器,@Reusable装饰器
android·app·harmonyos
余多多_zZ9 小时前
鸿蒙初学者学习手册(HarmonyOSNext_API14)_组件截图(@ohos.arkui.componentSnapshot (组件截图) )
学习·华为·harmonyos·鸿蒙·鸿蒙系统
一个处女座的程序猿O(∩_∩)O11 小时前
鸿蒙开发技术指南:从入门到精通
华为·harmonyos
陈无左耳、13 小时前
HarmonyOS学习第4天: DevEco Studio初体验
学习·华为·harmonyos
月未央15 小时前
HarmonyOS Next 开发系列:Local 状态管理实践
harmonyos
openinstall全渠道统计1 天前
免填邀请码工具:赋能六大核心场景,重构App增长新模型
android·ios·harmonyos