在 iOS 设备中以 react-native 播放声音
Play sound in iOS device in react-native
在我的应用程序中,我有一个功能可以在收到消息时播放声音,它会通过播放声音文件来通知用户,该应用程序使用 react-native-sound 在 Android 中很好,但在 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()
有人知道怎么解决吗?
来自 react-native-sound
文档:
iOS: Open Xcode and add your sound files to the project (Right-click
the project and select Add Files to [PROJECTNAME]).
然后就可以调用了
new Sound('Page_Turn.mp3',
如果使用require(filepath)
,则不需要Sound.MAIN_BUNDLE
,第二个参数是回调:
new Sound(require("../../res/audio/Page_Turn.mp3"), (error) => {})
play
函数应该在初始化成功后调用
var message_received = new Sound(require("../../res/audio/Page_Turn.mp3"), (error) => {
如果(错误){
console.log('failed to load the sound', 错误);
return;
}
//加载成功
message_received.play()
});
记得将您的 .mp3 文件添加(引用)到 xcode,重新 运行 react-native run-ios
。
此代码示例包含许多用例:https://github.com/zmxv/react-native-sound-demo/blob/master/main.js
在我的应用程序中,我有一个功能可以在收到消息时播放声音,它会通过播放声音文件来通知用户,该应用程序使用 react-native-sound 在 Android 中很好,但在 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()
有人知道怎么解决吗?
来自 react-native-sound
文档:
iOS: Open Xcode and add your sound files to the project (Right-click the project and select Add Files to [PROJECTNAME]).
然后就可以调用了
new Sound('Page_Turn.mp3',
如果使用
require(filepath)
,则不需要Sound.MAIN_BUNDLE
,第二个参数是回调:new Sound(require("../../res/audio/Page_Turn.mp3"), (error) => {})
play
函数应该在初始化成功后调用var message_received = new Sound(require("../../res/audio/Page_Turn.mp3"), (error) => { 如果(错误){ console.log('failed to load the sound', 错误); return; } //加载成功 message_received.play() });
记得将您的 .mp3 文件添加(引用)到 xcode,重新 运行
react-native run-ios
。
此代码示例包含许多用例:https://github.com/zmxv/react-native-sound-demo/blob/master/main.js