[英]Play sound in iOS device in react-native
In my app I have a feature that plays a sound when a message is received it will notify the user by playing a sound file, the app works using react-native-sound fine in Android but in iOS I keep getting this error:在我的应用程序中,我有一个功能,可以在收到消息时播放声音,它会通过播放声音文件通知用户,该应用程序在 Android 中使用react-native-sound 工作正常,但在 iOS 中我不断收到此错误:
Code I am using for initializing the sound:我用于初始化声音的代码:
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());
});
The method that I call after initializing sound file:我在初始化声音文件后调用的方法:
message_received.play()
Does anyone know how to solve this?有谁知道如何解决这个问题?
If you use require(filepath)
, you don't need Sound.MAIN_BUNDLE
, the second param is callback:如果您使用
require(filepath)
,则不需要Sound.MAIN_BUNDLE
,第二个参数是回调:
new Sound(require("../../res/audio/Page_Turn.mp3"), (error) => {})新声音(需要(“../../res/audio/Page_Turn.mp3”),(错误)=> {})
play
function should be called after initialising successfully初始化成功后应调用
play
函数
var message_received = new Sound(require("../../res/audio/Page_Turn.mp3"), (error) => { if (error) { console.log('failed to load the sound', error); return; } // loaded successfully message_received.play() }); var message_received = new Sound(require("../../res/audio/Page_Turn.mp3"), (error) => { if (error) { console.log('加载声音失败', error) ; return; } // 加载成功 message_received.play() });
Remember to add(references) your .mp3 files into xcode, re-run react-native run-ios
.请记住将您的 .mp3 文件添加(引用)到 xcode 中,重新运行
react-native run-ios
。
This code example contains many use-cases: https://github.com/zmxv/react-native-sound-demo/blob/master/main.js此代码示例包含许多用例: https : //github.com/zmxv/react-native-sound-demo/blob/master/main.js
From react-native-sound
doc:来自
react-native-sound
文档:
iOS: Open Xcode and add your sound files to the project (Right-click the project and select Add Files to [PROJECTNAME]).
iOS:打开 Xcode 并将您的声音文件添加到项目中(右键单击项目并选择将文件添加到 [PROJECTNAME])。
Then you can call it然后你可以调用它
new Sound('Page_Turn.mp3',
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.