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++;
}
相关推荐
前端不太难1 小时前
一套鸿蒙 App,如何跑在手机 / 平板 / TV?
智能手机·电脑·harmonyos
想你依然心痛1 小时前
HarmonyOS 6(API 23)实战:基于悬浮导航、沉浸光感与HMAF的“灵犀智脑“——PC端AI智能体工作流编排平台
人工智能·华为·harmonyos·智能体
轻口味1 小时前
HarmonyOS 6.1 全栈实战录 - 10 极光星图:Map Kit 6.1 3D地球、城市灯光与Marker碰撞深度实战
3d·华为·harmonyos
枫叶丹42 小时前
【HarmonyOS 6.0】Device Security Kit安全审计阻断功能深度解析
开发语言·安全·华为·harmonyos
枫叶丹42 小时前
【HarmonyOS 6.0】Device Security Kit 深度解读:应用进程信息安全审计查询能力
开发语言·华为·harmonyos
nashane14 小时前
HarmonyOS 6学习:Web组件同层渲染事件处理与智能长截图实现
前端·学习·harmonyos·harmonyos 5
nashane14 小时前
HarmonyOS 6学习:Web组件同层渲染触摸事件与长截图拼接实战
前端·学习·harmonyos·harmonyos 5
特立独行的猫a15 小时前
鸿蒙 PC 命令行工具迁移实战直播课 · pngquant命令行移植实战
华为·ai·harmonyos·vcpkg·鸿蒙pc·lycim
音视频牛哥16 小时前
鸿蒙NEXT如何接入GB28181平台?SmartMediaKit 设备接入集成实践
华为·harmonyos·鸿蒙next gb28181·鸿蒙gb28181设备对接·鸿蒙next对接gb28181·鸿蒙gb28181实时回传·鸿蒙next 28181对接
KKei163817 小时前
Flutter for OpenHarmony 学习视频播放器技术文章
学习·flutter·华为·音视频·harmonyos