Playing sound using require in iOS causes error "onError is not a function"
When running on app on iOS I get this error:

Code being used for initializing sound file:
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());
});
Code for playing sound file:
message_received.play()
Hmm, try removing Sound.MAIN_BUNDLE?
I think require is not supported in iOS not sure why, but I end up adding the sound files in the project and use the following as a workaround:
var android_pageturn = "../../res/audio/Page_Turn.mp3";
var ios_pageturn = "Page_Turn.mp3"
if(Platform.OS == "android") {
message_received = new Sound(require(android_pageturn), 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());
});
}
else {
Sound.setMode("Default")
Sound.setCategory("Playback")
message_received = new Sound(ios_pageturn, Sound.MAIN_BUNDLE, async (e) => {
if(e) {
console.log(e)
}
})
}
message_received.play()
@paulmelnikow do you mean when using require I should remove Sound.MAIN_BUNDLE?
If require(...) does not return a string you should exclude Sound.MAIN_BUNDLE.
It probably depends on your tooling, however that is what’s working for me.
@paulmelnikow I tried that and it did not work.
message_received = new Sound(require("../../res/audio/Page_Turn.mp3"), (e) => {
if(e) {
console.log(e)
}
})
What is the error? Also, if you assign require("../../res/audio/Page_Turn.mp3") to a variable, what is it?
Any solution? actually this is my code and dosnt works :(
const play = (error, sound) => sound.play()
const Sounds = new Sound(
require('../../assets/sounds/Beep_Short_05_Sound_Effect_Mp3_261.mp3'),
(error) => play(error, Sounds),
)
@YisusMB Could you please open a new issue and fill out the full template?
@0x01Brain thanks a lot, i import my .mp3 file in xcode and try your code and it works ty!
you should add '' instead of Sound.MAIN_BUNDLE example usage:
const Sound3 = new Sound(require("path_of_file"), '', (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());
});
Sound3.play();