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++;
}
相关推荐
云和数据.ChenGuang15 分钟前
鸿蒙餐饮系统:全场景智慧餐饮新范式
人工智能·机器学习·华为·数据挖掘·harmonyos·鸿蒙·鸿蒙系统
Swift社区22 分钟前
AI 驱动 UI:鸿蒙 ArkUI 的新可能
人工智能·ui·harmonyos
KIHU快狐2 小时前
KIHU快狐|国产鸿蒙系统立式一体机RK3588芯片多点触控交互查询终端
华为·交互·harmonyos
●VON2 小时前
半小时从零开发鸿蒙记事本应用:AI辅助开发实战
人工智能·华为·harmonyos
KIHU快狐5 小时前
KIHU快狐|电容触摸壁挂一体机鸿蒙信发系统国产芯片显示终端
华为·harmonyos
弓.长.5 小时前
ReactNative for OpenHarmony项目鸿蒙化三方库:react-native-flip-card — 翻转卡片组件
react native·react.js·harmonyos
弓.长.5 小时前
ReactNative for OpenHarmony项目鸿蒙化三方库:react-native-shake — 摇一摇事件监听
react native·react.js·harmonyos
大雷神5 小时前
HarmonyOS APP<玩转React>开源教程十七:模块详情页面
harmonyos
程序员大辉5 小时前
KaihongOS 5.0:免费的鸿蒙 X86 桌面系统,普通电脑也能装
华为·电脑·harmonyos
大雷神5 小时前
HarmonyOS APP<玩转React>开源教程十八:课程详情页面
前端·react.js·开源·harmonyos