我的应用程序中有一个功能,当收到消息时会播放声音,它将通过播放声音文件通知用户,该应用程序在Android上使用 react-native-sound 正常运行,但在iOS中,我不断收到此错误:
我用来初始化声音的代码:
import Sound from "react-native-sound"
// Load the sound file 'whoosh.mp3' from the app bundle
// See notes below about preloading sounds within initialization code below.
var message_received = new Sound(require("../../res/audio/Page_Turn.mp3"), Sound.MAIN_BUNDLE, (error) => {
if (error) {
console.log('failed to load the sound', error);
return;
}
// loaded successfully
console.log('duration in seconds: ' + message_received.getDuration() + 'number of channels: ' + message_received.getNumberOfChannels());
});
初始化声音文件后调用的方法:
message_received.play()
有人知道如何解决吗?
答案 0 :(得分:1)
来自react-native-sound
文档:
iOS:打开Xcode并将声音文件添加到项目中(右键单击 项目,然后选择将文件添加到[PROJECTNAME])。
然后您可以调用它
new Sound('Page_Turn.mp3',
答案 1 :(得分:0)
如果使用require(filepath)
,则不需要Sound.MAIN_BUNDLE
,第二个参数是回调:
新声音(require(“ ../../ res / audio / Page_Turn.mp3”),(错误)=> {})
play
函数
var message_received =新声音(require(“ ../../ res / audio / Page_Turn.mp3”),(错误)=> { 如果(错误){ console.log('无法加载声音',错误); 返回; } //成功加载 message_received.play() });
请记住将.mp3文件添加(引用)到xcode中,然后重新运行react-native run-ios
。
此代码示例包含许多用例:https://github.com/zmxv/react-native-sound-demo/blob/master/main.js