Javascript 在iOS设备中以react native播放声音
在我的应用程序中,我有一个功能,在收到消息时播放声音,它将通过播放声音文件通知用户,该应用程序在Android中使用react native sound正常工作,但在iOS中,我不断收到此错误: 我用于初始化声音的代码:Javascript 在iOS设备中以react native播放声音,javascript,ios,react-native,react-native-sound,Javascript,Ios,React Native,React Native Sound,在我的应用程序中,我有一个功能,在收到消息时播放声音,它将通过播放声音文件通知用户,该应用程序在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
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()
有人知道如何解决这个问题吗?来自
react native sound
doc:
iOS:打开Xcode并将声音文件添加到项目中(右键单击
打开项目并选择将文件添加到[PROJECTNAME])
那你可以叫它
新声音('Page\u Turn.mp3',
require(filepath)
,则不需要Sound.MAIN\u BUNDLE
,第二个参数是回调:
新声音(需要(“../../res/audio/Page_Turn.mp3”),(错误)=>{}play
函数应在初始化成功后调用
var message_received=新声音(需要(“../../res/audio/Page_Turn.mp3”),(错误)=>{
如果(错误){
console.log('加载声音失败',错误);
返回;
}
//已成功加载
消息_已收到。播放()
}))react native run ios
此代码示例包含许多用例:来自react native sound doc:iOS:打开Xcode并将您的声音文件添加到项目中(右键单击项目并选择将文件添加到[PROJECTNAME])。然后您可以像调用新声音一样调用它('Page_Turn.mp3',我这样做了,但仍然会收到相同的错误。执行此操作时会出现错误。play()或者当你启动声音时?@llario你的解决方案成功了谢谢!太好了!你能接受我的回答吗?:)