HarmonyOS鸿蒙Next中使用SoundPool播放音频,音频文件放在工程下的rawFile下,如何转化为参数uri?

HarmonyOS鸿蒙Next中使用SoundPool播放音频,音频文件放在工程下的rawFile下,如何转化为参数uri? 使用SoundPool播放音频,音频文件放在工程下的rawFile下,请问如何转化为参数uri?

3 回复

用SoundPool实现音频播放功能。

1、调用createSoundPool方法创建SoundPool实例。

import media from '@ohos.multimedia.media';
import audio from '@ohos.multimedia.audio';
import { BusinessError } from '@ohos.base';

let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage : audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags : 0
}

media.createSoundPool(5, audioRendererInfo).then((soundpool_: media.SoundPool) => {
  if (soundpool_ != null) {
    soundPool = soundpool_;
    console.info('create SoundPool success');
  } else {
    console.error('create SoundPool fail');
  }
}).catch((error: BusinessError) => {
  console.error(`soundpool catchCallback, error message:${error.message}`);
});
注册监听(资源加载完成,播放完成,错误类型监听)
soundPool.on('loadComplete', (soundId: number) => {
  console.info('loadComplete, soundId: ' + soundId);
});
soundPool.on('playFinished', () => {
  console.info("receive play finished message");
});
soundPool.on('error', (error) => {
  console.info('error happened,message is :' + error.message);
});
调用load方法进行音频资源加载。
可以传入uri或fd加载资源,此处使用传入uri的方式为例,更多方法请参考API文档。

const audioViewPicker = new picker.AudioViewPicker();
audioViewPicker.select(audioSelectOptions).then(async (audioSelectResult: Array<string>) => {
  urii = audioSelectResult[0];
  console.info('audioViewPicker.select to file succeed and uri is:' + urii);
  if(urii){
    await fs.open(urii, fs.OpenMode.READ_ONLY).then((file_: fs.File) => {
      let file=file_;
      console.info("file fd: " + file.fd);
      uri = 'fd://' + (file.fd).toString()
    }); // '/test_01.mp3' 作为样例,使用时需要传入文件对应路径。
    soundPool.load(uri).then((soundIdd: number) => {
      console.info('soundPool load uri success');
      soundId=soundIdd;
    }).catch((err: BusinessError) => {
      console.error('soundPool load failed and catch error is ' + err.message);
    })
  }

}).catch((err: BusinessError) => {
  console.error(`Invoke audioViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
})

2、配置播放参数PlayParameters,并调用play方法播放音频。多次调用play播放同一个soundID,只会播放一次。

let PlayParameters: media.PlayParameters = {
  loop: 3, // 循环4次
  rate: audio.AudioRendererRate.RENDER_RATE_NORMAL, // 正常倍速
  leftVolume: 0.9, // range = 0.0-1.0
  rightVolume: 0.9, // range = 0.0-1.0
  priority: 3, // 最低优先级
}

// 开始播放,这边play也可带播放播放的参数PlayParameters
streamId = await soundPool.play(soundId,PlayParameters);
释放资源
async function release() {
  // 终止指定流的播放
  soundPool.stop(streamId);
  // 卸载音频资源
  await soundPool.unload(soundId);
  //关闭监听
  setOffCallback();
  // 释放SoundPool
  await soundPool.release();
}
//关闭监听
function setOffCallback() {
  soundPool.off('loadComplete');
  soundPool.off('playFinished');
  soundPool.off('error');
}

更多关于HarmonyOS鸿蒙Next中使用SoundPool播放音频,音频文件放在工程下的rawFile下,如何转化为参数uri?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中使用SoundPool播放音频时,若音频文件存放在工程的rawFile目录下,需将其转化为uri参数。可以通过ResourceManager获取资源文件的uri。具体步骤如下:

  1. 获取ResourceManager实例:通过context.getResourceManager()获取ResourceManager对象。
  2. 获取资源uri:使用ResourceManagergetRawFileEntry方法获取RawFileEntry对象,再调用getUri()方法获取uri

示例代码如下:

import resourceManager from '@ohos.resourceManager';

let context = ...; // 获取上下文
let resourceMgr = context.resourceManager;
let rawFileEntry = resourceMgr.getRawFileEntry("entry/resources/rawfile/your_audio_file.mp3");
let uri = rawFileEntry.getUri();

获取到uri后,可将其传递给SoundPool进行音频播放。

在HarmonyOS鸿蒙Next中,如果你想将rawFile目录下的音频文件转化为uri以便通过SoundPool播放,可以使用ResourceManager来获取资源URI。具体步骤如下:

  1. 获取ResourceManager实例

    ResourceManager resourceManager = getResourceManager();
    
  2. 获取资源ID

    int resId = resourceManager.getResourceIdByName("your_audio_file_name", ResourceTable.Type_RAW);
    
  3. 生成URI

    String uri = "resource:///" + resId;
    
  4. 使用SoundPool播放音频

    SoundPool soundPool = new SoundPool.Builder().build();
    int soundId = soundPool.load(uri, 1);
    soundPool.play(soundId, 1.0f, 1.0f, 1, 0, 1.0f);
    

这样,你就可以使用生成的URI通过SoundPool播放音频了。

回到顶部