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++;
}
相关推荐
key_3_feng26 分钟前
HarmonyOS 6.0 健康食谱应用开发方案
华为·harmonyos
麒麟ZHAO38 分钟前
鸿蒙flutter第三方库适配 - 文件对比工具
数据库·redis·flutter·华为·harmonyos
互联网散修39 分钟前
零基础鸿蒙应用开发第三十四节:MVVM架构下的商品管理登录页
架构·harmonyos·mvvm·登录
弓.长.1 小时前
ReactNative for OpenHarmony项目鸿蒙化三方库:react-native-svg(CAPI) — 矢量图形组件
react native·react.js·harmonyos
不爱吃糖的程序媛1 小时前
鸿蒙三方库适配HPKCHECK 文件执行流程详解
华为·harmonyos
见山是山-见水是水2 小时前
Flutter 框架跨平台鸿蒙开发 - 电子发票智能管理
flutter·华为·harmonyos
HarmonyOS_SDK2 小时前
化繁为简:顺丰速运App如何通过 HarmonyOS SDK实现专业级空间测量
harmonyos
不爱吃糖的程序媛2 小时前
鸿蒙三方库适配读懂 `HPKBUILD`:lycium 怎么知道「下载谁、怎么编、装到哪」?
服务器·华为·harmonyos
李游Leo3 小时前
别让压图拖垮首帧:系统 Picker + TaskPool + ImagePacker,把 HarmonyOS 图片整理链路做顺
harmonyos
2401_839633913 小时前
鸿蒙flutter第三方库适配 - 存储空间分析
flutter·华为·harmonyos