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 小时前
HarmonyOS7 拖拽事件实战:从 onPreDrag 八阶段到 onDrop 投放(附完整案例)
harmonyos
杜子不疼.4 小时前
从 Apache HTTP Server 到 CivetWeb:在鸿蒙 PC 的 3.1 MiB HAP 里嵌入 Web 服务器
前端·apache·harmonyos
默 语10 小时前
HarmonyOS7 焦点事件实战:onFocus、onBlur 与走焦控制(附完整案例)
harmonyos
钉先生贾10 小时前
HarmonyOS 7.0 性能排障实战 43:图片与大资源加载抖动时,怎样区分解码、布局与状态更新
java·前端·harmonyos
默 语10 小时前
HarmonyOS7 点击事件实战:onClick、ClickEvent 与防误触(附完整案例)
harmonyos
默 语11 小时前
HarmonyOS7 快捷键事件实战:keyboardShortcut 从组合键到功能键(附完整案例)
harmonyos
我闪了个去12 小时前
给联系人存农历生日,App 里也能做了:HarmonyOS 7 联系人 SignificantDate 上手,附“今年哪天过生日”换算模板
harmonyos·arkts
OH_TPC12 小时前
@ohos/ffmpeg-kit 源码解读(一):aki 绑定层与线程模型——JSBIND 注册、线程池与跨线程回调
harmonyos
码间留白12 小时前
鸿蒙推送偶现静默通知(标题显示 Title、落进更多通知)的定位与修复:区分厂商通道与 TIMPush 本地补发
前端·uniapp·harmonyos·鸿蒙·推送·harmonyos next·push kit·timpush
默 语15 小时前
HarmonyOS7 软键盘事件实战:onNeedSoftkeyboard 与键盘接续(附完整案例)
计算机外设·harmonyos