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++;
}
相关推荐
坚果的博客2 小时前
技术解析:鸿蒙 PC 为什么采用 aarch64 架构?
华为·架构·harmonyos
ifeng09183 小时前
HarmonyOS实战项目:AI健康助手(影像识别与健康分析)
人工智能·华为·wpf·harmonyos
爱笑的眼睛113 小时前
HarmonyOS NFC应用开发:构建分布式近场通信解决方案
华为·harmonyos
星释9 小时前
鸿蒙Flutter三方库适配指南:09.版本升级适配
flutter·华为·harmonyos
CN-cheng1 天前
Flutter项目在HarmonyOS(鸿蒙)运行报错问题总结
flutter·华为·harmonyos·flutter运行到鸿蒙
平平不平凡1 天前
鸿蒙音频播放器深度解析:从核心设计到完整实现
harmonyos
HMSCore1 天前
【FAQ】HarmonyOS SDK 闭源开放能力 — Form Kit
harmonyos
爱笑的眼睛112 天前
HarmonyOS数据存储Kit深度实践:从架构设计到性能优化
华为·harmonyos
爱笑的眼睛112 天前
HarmonyOS后台代理提醒Agent:构建智能提醒功能的深度解析
华为·harmonyos
爱笑的眼睛112 天前
ArkTS可选链与空值合并:提升HarmonyOS应用开发的安全性与简洁性
华为·harmonyos