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++;
}
相关推荐
KKei16382 分钟前
Flutter for OpenHarmony 编程技能树APP技术文章
flutter·华为·harmonyos
想你依然心痛7 分钟前
HarmonyOS 6(API 23)实战:基于Face AR呼吸监测与Body AR姿态引导的“静界空间“——PC端沉浸式冥想疗愈系统
华为·ar·harmonyos·悬浮导航·沉浸光感
KKei163815 分钟前
Flutter for OpenHarmony 个人财务管理与记账APP
flutter·华为·harmonyos
nashane35 分钟前
HarmonyOS 6学习:Web组件与JavaScript交互的三大高频问题与终极解决方案
前端·学习·harmonyos
Swift社区44 分钟前
鸿蒙 PC 构建体系详解:从 DevEco 到发布
华为·harmonyos
KKei16381 小时前
Flutter for OpenHarmony 本地音乐播放器APP
flutter·华为·harmonyos
largecode1 小时前
怎么让手机显示公司名?来电显示公司名称认证实现品牌外显
linux·ubuntu·华为od·华为·智能手机·华为云·harmonyos
KKei16381 小时前
Flutter for OpenHarmony 外语单词背诵与听力训练APP
flutter·华为·harmonyos
前端不太难1 小时前
AI Native 鸿蒙 App 的四层架构
人工智能·架构·harmonyos
云和数据.ChenGuang2 小时前
HarmonyOS 手机模拟器开发「随身猜谜语小游戏」的技术实现方案
华为·智能手机·harmonyos