React-native-sound: onError is not a function

Created on 27 Apr 2017  路  7Comments  路  Source: zmxv/react-native-sound

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?

Most helpful comment

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

All 7 comments

@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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

meghaptg picture meghaptg  路  17Comments

adrian621 picture adrian621  路  15Comments

joshbalfour picture joshbalfour  路  17Comments

zakster12 picture zakster12  路  16Comments

facuacostag picture facuacostag  路  10Comments