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++;
}
相关推荐
别说我什么都不会6 分钟前
【仓颉三方库】工具类——zip4cj & zlib4cj
harmonyos
高木的小天才12 小时前
鸿蒙中的并发线程间通信、线程间通信对象
前端·华为·typescript·harmonyos
脑极体15 小时前
开源鸿蒙,给机器人带来了什么?
华为·机器人·开源·harmonyos
HarmonyOS_SDK17 小时前
意图框架事件推荐方案,精准匹配用户需求
harmonyos
Tolitres17 小时前
「鸿蒙 NEXT」基于 taskpool 实现自定义 Timer 工具类
harmonyos
城中的雾18 小时前
一键多环境构建——用 Hvigor 玩转 HarmonyOS Next
harmonyos
别说我什么都不会18 小时前
【仓颉三方库】分布式——config-client
harmonyos
咸鱼过江18 小时前
openharmony5.0.0中C++公共基础类测试-线程相关(一)
c++·harmonyos
我爱鸿蒙开发20 小时前
🥇聊聊鸿蒙的一端开发,多端部署。
前端·开源·harmonyos
智驾1 天前
HarmonyOS 是 Android 套壳嘛?
android·harmonyos·替代·套壳