The song file is being found but I'm getting to an error "onError is not a function" referencing sound.js line 46.
Any ideas anyone?
@kfaria can you provide an example of your code?
@trepidity I'm also running into this error. Here's my code. When I run this code I get the error mentioned above by @kfaria. I've also tried setting the second argument to null in which case I don't get the error but no sound plays.
const sound = new Sound(
require('../assets/chimeSounds/singing-bowl.mp3'),
Sound.MAIN_BUNDLE,
(error) => {
if (error) {
console.log('error occured', error)
}
},
)
sound.play()
According to the react-native-sound-demo if require is being used, the second parameter needs to be a callback:
// If the audio is a 'require' then the second parameter must be the callback.
if (testInfo.isRequire) {
const sound = new Sound(testInfo.url, error => callback(error, sound));
} else {
const sound = new Sound(testInfo.url, testInfo.basePath, error => callback(error, sound));
}
see: https://github.com/zmxv/react-native-sound-demo/blob/master/main.js#L142
I think this needs some clarification in de documentation.
@gvenk thanks. I'm able to play the sound with the following:
const play = (error, sound) => sound.play()
const sound = new Sound(
require('../assets/chimeSounds/singing-bowl.mp3'),
(error) => play(error, sound),
)
This is an odd api for the simple task of playing a song. Something like the following seems much more intuitive:
Sound(require('../assets/chimeSounds/singing-bowl.mp3'))
.then((sound, error) => {
if (error) return console.log(error)
sound.play()
})
@smkhalsa I agree the API could be better, think this is 'historically' grown. Maybe someday there will be a version 2 with a new, cleaner API 馃槈
Good points to you both!
Most helpful comment
According to the react-native-sound-demo if require is being used, the second parameter needs to be a callback:
see: https://github.com/zmxv/react-native-sound-demo/blob/master/main.js#L142