HarmonyOS鸿蒙Next中实现点击按钮发出声音时出现“soundPool load failed and catch error is No such file or directory”错误

HarmonyOS鸿蒙Next中实现点击按钮发出声音时出现“soundPool load failed and catch error is No such file or directory”错误

let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    return;
  } else {
    let file: fs.File;
    let soundID: number = 0;
    let fileSize: number = 1; //通过fs.stat()获取size值
    let uri: string = "";
    //获取fd的描述信息
    fs.open('/assets/addsub.mp3', fs.OpenMode.READ_ONLY).then((file_: fs.File) => {
      file = file_;
      console.info("file fd: " + file.fd);
      uri = 'fd://' + (file.fd).toString()
      soundPool = soundPool_;
      soundPool.load(uri).then((soundId: number) => {
        console.info('soundPool load uri success');
        soundID = soundId;
        console.info(`createSoundPool success`)
        let playParameters: media.PlayParameters = {
          loop: 1, // 循环4次
          rate: audio.AudioRendererRate.RENDER_RATE_NORMAL, // 正常倍速
          leftVolume: 0.5, // range = 0.0-1.0
          rightVolume: 0.5, // range = 0.0-1.0
          priority: 0, // 最低优先级
        }
        soundPool.play(soundId, playParameters).then((streamId: number) => {
          console.info('play success');
        }, (err: BusinessError) => {
          console.error('soundpool play failed and catch error is ' + err.message);
        });
      })
    }, (err: BusinessError) => {
      console.error('soundPool load failed and catch error is ' + err.message);
    });

  }
})

更多关于HarmonyOS鸿蒙Next中实现点击按钮发出声音时出现“soundPool load failed and catch error is No such file or directory”错误的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

参考demo

import { media } from '@kit.MediaKit';
import { audio } from '@kit.AudioKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';

@Component
struct BorderExample3 {
  private context: common.UIAbilityContext = this.getContext(this) as common.UIAbilityContext;

  build() {
    Button("测试").onClick(() => {
      let soundPool: media.SoundPool;
      let audioRendererInfo: audio.AudioRendererInfo = { usage: audio.StreamUsage.STREAM_USAGE_MUSIC, rendererFlags: 1 };
      media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
        if (error) {
          return;
        } else {
          console.info(`createSoundPool success`);
          this.setCallBack(soundPool_);
          let file = this.context.resourceManager.getRawFdSync('assets/addsub.mp3');
          soundPool = soundPool_;
          soundPool.load(file.fd, file.offset, file.length).then((soundId: number) => {
            console.info('soundPool load uri success');
          });
        }
      });
    });
  }

  setCallBack(soundPool: media.SoundPool) {
    soundPool.on('loadComplete', (soundId: number) => {
      console.info(`[SoundPoolDemo] loadComplete success,soundId:${soundId}`);
      let playParameters: media.PlayParameters = {
        loop: 1, // 循环4次
        rate: audio.AudioRendererRate.RENDER_RATE_NORMAL, // 正常倍速
        leftVolume: 0.5, // range = 0.0-1.0
        rightVolume: 0.5, // range = 0.0-1.0
        priority: 0, // 最低优先级
      };
      soundPool.play(soundId, playParameters).then((streamId: number) => {
        console.info('play success');
      }, (err: BusinessError) => {
        console.error(`soundpool play failed and catch error is ${err.message}`);
      });
    });

    soundPool.on('playFinished', () => {
      console.info(`[SoundPoolDemo] playFinished success`);
    });

    soundPool.on('error', (error: BusinessError) => {
      console.error(`error happened,and error is ${JSON.stringify(error)}`);
    });
  }
}

更多关于HarmonyOS鸿蒙Next中实现点击按钮发出声音时出现“soundPool load failed and catch error is No such file or directory”错误的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,出现“soundPool load failed and catch error is No such file or directory”错误通常是由于文件路径错误或文件不存在导致的。检查以下几点:

  1. 文件路径:确保声音文件的路径正确,路径应使用绝对路径或相对路径,并包含文件名和扩展名。

  2. 文件存在:确认声音文件存在于指定路径中,文件名和扩展名需与代码中一致。

  3. 文件格式:确保声音文件格式为系统支持的格式,如WAV或MP3。

  4. 资源管理:若声音文件放在资源目录中,确保资源管理配置正确,如resources/base/media目录。

  5. 权限配置:确保应用已获取读取外部存储的权限,需在config.json中配置ohos.permission.READ_MEDIA权限。

  6. 文件大小:检查声音文件大小,过大的文件可能导致加载失败。

  7. 代码逻辑:检查SoundPool加载代码,确保load方法调用正确,参数无误。

通过检查这些方面,可以解决该错误。

在HarmonyOS鸿蒙Next中,出现“soundPool load failed and catch error is No such file or directory”错误,通常是因为文件路径错误或文件不存在。请检查以下几点:

  1. 确保音频文件路径正确,且文件存在于指定位置。
  2. 使用ResourceManager加载资源时,确保资源ID正确。
  3. 检查文件格式是否支持,如.mp3.wav
  4. 确保应用有读取文件的权限。
  5. 使用try-catch捕获异常,查看详细错误信息。
回到顶部