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 小时前
【鸿蒙 PC三方库构建系统】SHA 库 鸿蒙PC 适配详解
华为·harmonyos
seal_jing1 小时前
44岁被裁后用AI写鸿蒙App(3):AGC 认证 + 云函数 + 端到端加密实战
harmonyos
程序猿追3 小时前
画个饼,给数据点颜色看看——在 HarmonyOS 模拟器上手搓一个饼图/环形图组件
深度学习·算法·harmonyos
不爱吃糖的程序媛4 小时前
鸿蒙 6 新华字典实战:从零到一用 ArkTS 开发原生鸿蒙应用
华为·ar·harmonyos
再见6584 小时前
HarmonyOS NEXT 实战:开发一个实用的文本分析器应用
华为·harmonyos
程序猿追5 小时前
给手机做一次“体检”——我在 HarmonyOS 上写了个存储空间与内存查看器
华为·智能手机·harmonyos
nashane5 小时前
HarmonyOS 6学习:HAR包跨平台编译陷阱与架构优化实战
学习·华为·harmonyos
坚果的博客5 小时前
ycium_plusplus 项目全景解读:OpenHarmony 三方库构建的“大管家“
华为·harmonyos
李二。6 小时前
鸿蒙原生ArkTS-系外行星百科AI
人工智能·华为·harmonyos
想你依然心痛6 小时前
HarmonyOS 6(API 23)实战:构建“光愈冥想舱“——智能情绪疗愈系统
华为·ar·harmonyos·智能体