HarmonyOS 使用SoundPool无法播放rawfile下的mp3音频文件

我使用的 SoundPool 想要播放 resources\rawfile 下的mp3文件,照demo写好了,但是不能播放。

// 加载音频资源 
  await fs.open('../../resources/rawfile/alarm.mp3', fs.OpenMode.READ_ONLY).then((file: fs.File) => { 
    console.info("file fd: " + file.fd); 
    uri = 'fd://' + (file.fd).toString(); 
  }); // '/test_01.mp3' 作为样例,使用时需要传入文件对应路径。
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

请问该如何操作?

HarmonyOS
2024-11-13 11:13:47
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

可以参考下面demo:

import audio from '@ohos.multimedia.audio'; 
import media from '@ohos.multimedia.media'; 
import { BusinessError } from '@ohos.base'; 
import common from '@ohos.app.ability.common'; 
let soundPool: media.SoundPool; 
let streamIda: number = 0; 
let soundIda: number = 0; 
let audioRendererInfo: audio.AudioRendererInfo = { 
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 
  rendererFlags: 1 
} 
let PlayParameters: media.PlayParameters = { 
  loop: 0, // 循环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, // 最低优先级 
} 
@Entry 
@Component 
struct SoundPoolDemo { 
  @State message: string = '播放音频'; 
 
  async aboutToAppear(): Promise<void> { 
    create(); 
  } 
  async aboutToDisappear() { 
    release(); 
  } 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
        Button('play').onClick((event: ClickEvent) => { 
          PlaySoundPool1(); 
        }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 
async function create() { 
  //创建soundPool实例 
  soundPool = await media.createSoundPool(10, audioRendererInfo); 
  //注册监听 
  loadCallback(); 
  finishPlayCallback(); 
  setErrorCallback(); 
  CreatAudio('audio1.mp3') 
} 
 
async function CreatAudio(path:string) { 
  // 加载音频资源 
  let context = getContext(SoundPoolDemo) as common.UIAbilityContext; 
  let fileDescriptor = await context.resourceManager.getRawFd(path); 
  soundPool.load(fileDescriptor.fd,fileDescriptor.offset,fileDescriptor.length).then((soundId1: number) => { 
    console.info('soundPool load uri success 1'); 
    soundIda=soundId1; 
    console.log(`${soundId1}`) 
  }).catch((err: BusinessError) => { 
    console.error('soundPool load failed and catch error is ' + err.message); 
  }) 
} 
async function loadCallback() { 
  // 加载完成回调 
  soundPool.on('loadComplete', (soundId_: number) => { 
    console.info('loadComplete, soundId: ' + soundId_); 
  }) 
} 
//设置播放完成监听 
async function finishPlayCallback() { 
  // 播放完成回调 
  soundPool.on('playFinished', () => { 
    console.info("recive play finished message"); 
    // 可进行下次播放 
  }) 
} 
//设置错误类型监听 
function setErrorCallback() { 
  soundPool.on('error', (error) => { 
    console.error('error happened,message is :' + error.code); 
    console.error('error happened,message is :' + error.message); 
  }) 
} 
async function PlaySoundPool1() { 
  console.log("play"); 
  // 开始播放,这边play也可带播放播放的参数PlayParameters 
  streamIda = await soundPool.play(soundIda,PlayParameters); 
 
} 
 
async function unload(){ 
  //终止指定流的播放 
  soundPool.stop(streamIda); 
 
  // 卸载音频资源 
  await soundPool.unload(soundIda); 
} 
 
async function release() { 
  if (streamIda!=0) { 
    //终止指定流的播放 
    soundPool.stop(streamIda); 
    // 卸载音频资源 
    await soundPool.unload(soundIda); 
  } 
  //关闭监听 
  setOffCallback(); 
  // 释放SoundPool 
  await soundPool.release(); 
} 
//关闭监听 
function setOffCallback() { 
  soundPool.off('loadComplete'); 
  soundPool.off('playFinished'); 
  soundPool.off('error'); 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
分享
微博
QQ
微信
回复
2024-11-13 16:12:17
相关问题
HarmonyOS SoundPool播放rawfile音频失败
744浏览 • 1回复 待解决
HarmonyOS mp3播放与暂停
670浏览 • 1回复 待解决
HarmonyOS 是否支持MP3音频录制?
949浏览 • 1回复 待解决
HarmonyOS 播放本地音频文件
1462浏览 • 1回复 待解决
HarmonyOS如何从麦克风录制mp3文件
1752浏览 • 1回复 待解决
SoundPool实现音频播放功能
2212浏览 • 1回复 待解决