react-native-sound: Not working on IOS: Cannot read property 'IsAndroid' of undefined

The error is regarding the following line of code in sound.js :

var RNSound = require('react-native').NativeModules.RNSound;
var IsAndroid = RNSound.IsAndroid;

I have followed the instructions exactly as outlined for IOS, so I have no idea why I am getting this error. This module works perfectly on Android.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 17
  • Comments: 50 (4 by maintainers)

Most upvoted comments

none of the suggested solutions worked for me, running react-native link works, however it doesn’t seems to do anything, I had this error as well, and I was able to resolve it by manually adding RNSound.xcodeproj to Libraries folder and drag and drop libRNSound.a to Link Binary with Libraries of my project.

I had this error as well when adding the dependencies but forgetting to recompile. After a new build it worked like a charm! cc @Frosty92

Had the same experience as @DeanKamali - react-native link only linked to my first target, not the others. So just keep that in mind. In the end I manually selected my dev target and linked libRNSound.a by dragging (see docs), re-built from Xcode and it worked.

Unhandled JS Exception: undefined is not an object (evaluating ‘RNSound.IsAndroid’)

Deleting all files in iOS build folder and restarting the packager solved the issue

rm -rf ios/build/*

I have the same issue, and I tried reinstalling node module and recompiling, but it didn’t help. reinstalled by: npm install react-native-sound --save recompiled by: react-native run-ios

is this what you did? @will-ockmore

I get

undefined is not an object (evaluating '_reactNativeSound.Sound.MAIN_BUNDLE')

probably related. react-native link sets react-native-sound where the docs say RNSound. I changed that manually and the above error is what I end up with after copying a sound to the specified folder android/app/src/main/res/raw and running react-native run-android again.

import issue. After reading Sound.js it was an easy fix:

 // wrong
import {Sound} from 'react-native-sound'
 // right
import Sound from 'react-native-sound'
// or
import {default as Sound} from 'react-native-sound'

I was able to fix the issue undefined is not an object (evaluating 'RNSound.IsAndroid') on iOS 💥.

What I did was just to reinstall react-native-sound npm install react-native-sound --save and instead of linking it to xcode/android manually, I used react-native link react-native-sound to link it automatically, after that the error disappeared.


Just for reference, to play a sound just open xcode, and drag-drop your sound file into the root of the xcode project library:

--AppName
----Libraries
----AppNameTests
----Products
----Resources
----SoundFile.wav

Then tick Copy items if needed, Create folder references and in add to targets select your AppName, then click finish to add it to your project.

Now go to your Project Build Phases and make sure your sound file is listed on Copy Bundle Resources, if it’s not in there, add it by drag-drop your sound file.

Now everything should be ready, just don’t forget to run your app in xcode to build it.

Here’s a sample code I’m using in my React Native app:

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('tap.wav', 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>
    );
  }
}

And now it’s 100% working, I tried it on iOS only, but the original error should be gone on both platforms.

Just don’t forget to run the project with xcode, then you can use react-native run-ios, that’s all!

Hope this is helpful! 😊

I finally resolved this issue by following steps (I don’t know which one fixed the issue, so I’ll put everything I did)

  1. Kill processes on port 8081
  2. rm -rf ios/build/* (Delete all files in iOS build folder)
  3. Reboot
  4. Open up in XCode and change the scheme from building for a Release to Debug.

Now I see RNSound object on ReactNative.

In my case, I used:

npm i --save react-native-sound

and then

react-native link react-native-sound

But then I found under /node_modules/react-native-sound/, there is no ‘android’ folder.

So I downloaded ‘react-native-sound’ manually, copy the ‘android’ folder to /node_modules/react-native-sound/ and rebuilt. It seems no more error.

I solved this problem. Solution; close android / ios emulator and close android Studio / XCode and close terminal / cmd. Restart all and it worked.

@snsekar @Frosty92 @zmxv @will-ockmore @Roconda @sydneyitguy

Had the same issue on iOS & Android and was able to fix it.

iOS: My fault I did not choose my target correctly i.e. under built target I had myProjectTests selected rather than myProject. Once I fixed this I did not receive any error on iOS.

Android: According to RN docs RNSound package should be added to getPackages() method of MainApplication.java rather than MainActivity.java. Once I changed this the error went away.

I am using RN version 0.39.2.

Hope this helps.

Make sure that you run pod install after react-native link react-native-sound in case you are use pods. In other case link it manually https://github.com/zmxv/react-native-sound/issues/36#issuecomment-284443489

I only get the error when running unit tests! Building an playing sounds works perfectly fine. What could be the reason for it?

I’m able to solve this error, by re-linking the libraries again

react-native link
react-native run-ios

I had this issue on iOS briefly, just after running yarn add react-native-sound and react-native link.

I fixed it by recompiling. I forgot to do that 🙈. This is probably not the same issue as what others are experiencing though.