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++;
}
相关推荐
xyccstudio4 小时前
将 libsmb2 集成到 HarmonyOS ArkTS 项目
harmonyos
HwJack208 小时前
HarmonyOS 6APP开发之摸透ArkUI FrameNode
华为·harmonyos
求学中--10 小时前
状态管理一文通:@State、@Prop、@Link、@Provide/Consume全解析
人工智能·小程序·uni-app·wpf·harmonyos
求学中--10 小时前
ArkUI组件库完全指南:从基础组件到自定义装饰器
低代码·华为·小程序·uni-app·harmonyos
●VON10 小时前
鸿蒙原生APP开发实战指南:三套低成本AI辅助方案全解析
人工智能·华为·chatgpt·大模型·harmonyos·image
枫叶丹410 小时前
【HarmonyOS 6.0】Data Augmentation Kit 智慧化数据检索 C 接口解析:向量化、知识检索与知识问答
c语言·开发语言·华为·harmonyos
2301_8152795211 小时前
鸿蒙原生开发的“硬核通道”:ArkTS 与 C/C++ 高性能互操作全栈指南 —— FFI 机制深度解析与实战精要
c语言·c++·harmonyos
前端不太难1 天前
鸿蒙 App 的“无状态 System”设计
华为·状态模式·harmonyos
UnicornDev1 天前
【Flutter x HarmonyOS 6】魔方计时APP——计时逻辑实现
flutter·华为·harmonyos·鸿蒙·鸿蒙系统
AlbertZein2 天前
ImageKnifePro 源码解读:鸿蒙图片加载框架全貌
harmonyos