React-native-sound: Playing sound using require in iOS causes error "onError is not a function"

Created on 10 Jul 2019  路  9Comments  路  Source: zmxv/react-native-sound

When running on app on iOS I get this error:

image

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()

ios question

Most helpful comment

If require(...) does not return a string you should exclude Sound.MAIN_BUNDLE.

It probably depends on your tooling, however that is what鈥檚 working for me.

All 9 comments

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鈥檚 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 :(

````js
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!

Was this page helpful?
0 / 5 - 0 ratings