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 小时前
高德地图 Flutter 插件:跨 Android / iOS / HarmonyOS 的完整实现
harmonyos
狼哥16862 小时前
蛋糕美食元服务_地图实现指南
ui·harmonyos
JohnnyDeng944 小时前
【鸿蒙】HarmonyOS 数据持久化:Preferences/KV Store/RelationalStore 选型指南
harmonyos·arkts·鸿蒙·数据持久化·arkui
小雨青年4 小时前
【鸿蒙原生开发会议随记 Pro】用 NavPathStack 收拢会议页面跳转和返回刷新
华为·harmonyos
轻口味5 小时前
轻规划鸿蒙开发实战3:AR Engine Kit 深度实践,基于面部追踪与骨骼捕捉的体感微笑打
华为·ar·harmonyos·鸿蒙
Swift社区5 小时前
鸿蒙 App 为什么需要统一状态源?
华为·harmonyos
星释5 小时前
HDC 2026 跨平台框架专题:HarmonyOS 生态下的跨端技术全景
华为·harmonyos
加农炮手Jinx6 小时前
Flutter for OpenHarmony:pub_updater 命令行工具自动更新专家(DevOps 运维必备) 深度解析与鸿蒙适配指南
android·运维·网络·flutter·华为·harmonyos·devops
yuegu7776 小时前
HarmonyOS应用<节气通>开发第21篇:CategoryGrid组件封装
华为·harmonyos
yuegu7777 小时前
HarmonyOS应用<节气通>开发第25篇:HTTP请求封装
网络协议·http·harmonyos