我正在开发一个React Native应用程序,我需要从后端加载音频文件并在应用程序中播放。
为此,我使用包RNFetchBlob和react-native-audio-recorder-player。
问题是我的实现在Android上完美运行,但在iOS上不起作用......即使播放使用iOS本身内部的react-native-audio-recorder-player 录制的文件。
播放使用RNFetchBlob下载的文件时,出现以下错误:
FigFileForkOpenMainByCFURL signalled err=2 (errno) (open failed) at /Library/Caches/com.apple.xbs/Sources/EmbeddedCoreMediaFramework_Sim/EmbeddedCoreMedia-2765.6/Sources/Platform/Darwin/DarwinFile.c:576
重要的代码部分:
import AudioRecorderPlayer from 'react-native-audio-recorder-player';
import RNFetchBlob from 'rn-fetch-blob';
// THIS IS A SAMPLE, NOT THE REAL URL AND TOKEN.
const fileRemoteUrl = 'https://my-backend.com/files/file-id';
const authToken = 'my-authtoken';
// could be 'mp3' or 'aac', etc...
const fileExtension = 'm4a';
const dir = NFetchBlob.fs.dirs.DocumentDir;
const path = `${dir}/${Base64.btoa(fileRemoteUrl)}.${fileExtension}`;
const res = await RNFetchBlob.config({
fileCache: false,
appendExt: fileExtension,
path,
}).fetch('GET', fileRemoteUrl, { Authorization: `Bearer ${authToken}` });
const internalUrl = `${Platform.OS === 'android' ? 'file://' : ''}${res.path()}`;
const audioRecorderPlayer = new AudioRecorderPlayer();
await audioRecorderPlayer.startPlayer(internalUrl);
// here the audio should start playing,
// but I get the error on the device(simulator) console (xcode output)
正如我之前所说,相同的代码在Android上就像一个魅力。知道如何解决这个问题吗?我被困在这......我很感激任何帮助!