React Native中无法播放Firebase存储的MP3链接,iOS报OSStatus错误1954115647
解决React Native Sound在iOS加载远程音频的OSStatus 1954115647错误
这个错误码对应iOS的kAudioFileUnsupportedFileTypeError,本质是iOS无法正确识别或加载你的远程音频文件,以下是针对性的解决方案:
1. 显式指定音频文件类型
React Native Sound在iOS上加载远程URL时,有时无法自动推断文件格式,显式指定类型可以解决这个问题:
修改你的初始化代码,添加options参数指定fileType:
const audio = new Sound( 'https://storage.googleapis.com/chat_bucket_aso/2ihzd4q47l73wmvcb_2.976.mp3', null, { ios: { fileType: 'mp3' } }, // 显式设置文件类型 error => { setLoadingAudio(false); if (error) { console.log('failed to load the sound', error); setError(true); return; } setDuration(hp.recorderTimeFormat(audio.getDuration())); setPlayingDuration(audio.getDuration()); }, );
2. 调整iOS ATS配置
虽然你的URL是HTTPS,但如果App的ATS(App Transport Security)设置过于严格,可能会阻止加载。在Info.plist中添加对应域名的例外配置:
<key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>storage.googleapis.com</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSTemporaryExceptionMinimumTLSVersion</key> <string>TLSv1.2</string> </dict> </dict> </dict>
3. 检查音频文件编码兼容性
iOS的AVFoundation对MP3编码有严格限制,仅支持MPEG-1/MPEG-2 Layer 3格式,不支持MPEG-2.5 Layer 3。如果你的音频是后者,需要转码为标准MP3:
使用FFmpeg执行转码命令:
ffmpeg -i 2ihzd4q47l73wmvcb_2.976.mp3 -c:a libmp3lame -b:a 128k converted.mp3
替换原文件后再测试。
4. 升级React Native Sound到最新版本
旧版本的库存在iOS远程音频加载的已知bug,升级到最新稳定版可以修复:
npm install react-native-sound@latest --save
内容的提问来源于stack exchange,提问作者Aung Kyaw Soe
相关产品推荐
相关产品推荐

