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++;
}
相关推荐
前端不太难31 分钟前
鸿蒙 App 重构:如何从混乱到清晰?
重构·状态模式·harmonyos
听麟15 小时前
HarmonyOS 6.0+ PC端离线翻译工具开发实战:端侧AI模型集成与多格式内容翻译落地
人工智能·华为·harmonyos
南村群童欺我老无力.15 小时前
鸿蒙pc中权限申请流程与用户拒绝处理
华为·harmonyos
@不误正业15 小时前
AI-Agent安全性实战-提示注入防御与工具调用沙箱隔离
人工智能·华为·harmonyos
南村群童欺我老无力.19 小时前
鸿蒙PC多端适配的断点设计与布局策略
华为·harmonyos
轻口味20 小时前
HarmonyOS 6.1 全栈实战录 - 04 镜像世界:Spatial Recon Kit 3D空间重建与企业级高精度建模实战
3d·华为·harmonyos
酿情师21 小时前
2026平航杯倩倩手机逆向包逆向全过程(逆向鸿蒙系统app包)
华为·智能手机·harmonyos·逆向·ctf·re·取证
南村群童欺我老无力.21 小时前
鸿蒙PC DevEco Studio调试器的使用技巧与局限
华为·harmonyos