1

我有文件选择器从本地存储中选择音频文件,文件选择器的结果如下

size: 1057931
name: "AUD-20190314-WA0019.m4a"
type: "audio/mpeg"
uri: "content://com.android.externalstorage.documents/document/primary%3AWhatsApp%2FMedia%2FWhatsApp%20Audio%2FAUD-20190314-WA0019.m4a" 

我目前正在使用 react-native-sound 从选择器中播放选定的音频文件

var whoosh = new Sound(audio, Sound.MAIN_BUNDLE, error => {
  if (error) {
    console.log('failed to load the sound', error);
    return;
  }
  // loaded successfully
  console.log(
    'duration in seconds: ' +
      whoosh.getDuration() +
      'number of channels: ' +
      whoosh.getNumberOfChannels(),
  );

  // Play the sound with an onEnd callback
  whoosh.play(success => {
    if (success) {
      console.log('successfully finished playing');
    } else {
      console.log('playback failed due to audio decoding errors');
    }
  });
}

(音频变量是文件选择器中恢复的 uri)

但是这种方法不播放音频文件而不是播放,它给我留下了错误

无法加载声音{消息:“找不到资源”,代码:-1}

我做错了什么,我怎样才能播放从文件选择器中选择的音频文件?

4

1 回答 1

0

https://www.npmjs.com/package/react-native-track-player This package will allow you to play audio file which have been picked from local storage.If you wan to pick audio file from local storage you can use React native Document picker lib

于 2020-10-14T11:50:56.407 回答