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++;
}
相关推荐
aqi005 小时前
一文理清 HarmonyOS 6.0.2 涵盖的十个升级点
android·华为·harmonyos·鸿蒙·harmony
环信即时通讯云7 小时前
环信Flutter UIKit适配鸿蒙实战指南
flutter·华为·harmonyos
Swift社区8 小时前
鸿蒙 PC 应用启动优化全解析
华为·harmonyos
richard_yuu9 小时前
鸿蒙本地数据存储实战|Preferences 封装、数据隔离与隐私合规存储方案
android·华为·harmonyos
Lynnb10 小时前
Mac电脑烧录 RK3588 鸿蒙开发板固件教程
华为·harmonyos·鸿蒙系统
我头上有犄角ovo10 小时前
HarmonyOS 测肤拍照页实战:Metadata 实时取景 + Core Vision 拍后校验,从 0.001 的 widthRatio 踩坑到可上线
前端·harmonyos
码农小北11 小时前
MAC 配置鸿蒙(HarmonyOS) SDK 环境变量完整指南
macos·华为·harmonyos
weixin_3864689611 小时前
openharmony 6.0编译rk3568过程记录
c语言·c++·git·python·vim·harmonyos·openharmony
小雨青年11 小时前
HarmonyOS 6 | Pura X Max 鸿蒙原生适配 08:大屏下操作按钮位置重排
华为·harmonyos