Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RNSound.IsAndroid error (React Native)

Am getting this error- undefined is not an object(evaluating RNSound.IsAndroid)

I have already used this- react-native link react-native-sound

my index.android.js code is-

import React from 'react';
import { TouchableWithoutFeedback, Text } from 'react-native';
import Sound from 'react-native-sound';

class MyComponent extends Component {
  playSound() {
    const mySound = new Sound('x.mp3', Sound.MAIN_BUNDLE, (e) => {
      if (e) {
        console.log('error', e);
      } else {
        console.log('duration', mySound.getDuration());
        mySound.play();
      }
    });
  }

  render() {
    return (
      <TouchableWithoutFeedback onPress={this.playSound.bind(this)}>
         <Text>Play Sound!</Text>
      </TouchableWithoutFeedback>
    );
  }
}
like image 980
ARPIT PRASHANT BAHETY Avatar asked Jul 20 '17 16:07

ARPIT PRASHANT BAHETY


3 Answers

Most of the time this error means the package was not linked correctly.

To confirm if this is the case:-

  1. Go to android/app/src/main/java/.../MainApplication.java
  2. Ensure you have this import at this file import com.zmxv.RNSound.RNSoundPackage;
  3. Ensure this method has this call new RNSoundPackage() as illustrated below.

    @Override
    protected List<ReactPackage> getPackages() {
        return Arrays.<ReactPackage>asList(
            new MainReactPackage(),
            new RNSoundPackage() // <-- New
        );
    }
    
  4. If this doesn't work follow the manual process described in this wiki here: https://github.com/zmxv/react-native-sound/wiki/Installation

like image 52
kabangi julius Avatar answered Sep 28 '22 03:09

kabangi julius


I resolved this by stopping react packager, run following commands:

rm -rf node_modules/
npm install
react-native link react-native-sound
rn-nodeify --install --hack

clean project and re-build application. rn-nodeify was created for non-react native packages, but in my case this works for react-native-sound.

like image 32
Arkadiusz Wieczorek Avatar answered Sep 29 '22 03:09

Arkadiusz Wieczorek


Fixed this bug on react-native 0.61.5: add in Podfile

pod 'RNSound', :path => '../node_modules/react-native-sound'

pod install

like image 24
Tatsiana Avatar answered Sep 28 '22 03:09

Tatsiana