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++;
}
相关推荐
如此风景12 小时前
鸿蒙开发中 Model 类的组织方式
harmonyos
爱吃大芒果12 小时前
高密度数据可视化渲染:基于 ArkUI Grid 的状态热力图组件开发图纸
华为·信息可视化·harmonyos
心中有国也有家13 小时前
E-Brufen 技术选型全景:Flutter + 鸿蒙 + Hive CE
hive·hadoop·学习·flutter·华为·harmonyos
绝世番茄14 小时前
鸿蒙原生ArkTS布局方式之List+swipeAction侧滑菜单深度解析
华为·list·harmonyos·鸿蒙
Helen_cai15 小时前
OpenHarmony Preferences 本地持久化存储开发(API Version23 + 适配版)
华为·harmonyos
千逐6815 小时前
UniApp 鸿蒙实战:音视频与多媒体处理完全指南
华为·uni-app·harmonyos·鸿蒙
国服第二切图仔16 小时前
HarmonyOS APP《画伴梦工厂》开发第51篇-Skill开发入门——VibeCoding与系统级智能入口
华为·harmonyos
哦***716 小时前
最后一块拼图补齐!华为大豆包时代开启✨
华为·音频·harmonyos
在书中成长17 小时前
HarmonyOS 小游戏《对战五子棋》开发第11篇 - AIPlayer设计概览:三档难度架构
华为·harmonyos
最爱老式锅包肉17 小时前
《HarmonyOS技术精讲-ArkGraphics 2D(方舟2D图形服务)》第七篇:动效来袭——与ArkUI结合实现2D动画与交互
华为·交互·harmonyos