admin管理员组

文章数量:1025518

I am trying really hard to play an audio file from my react-native app. Currenty I am trying to use this package:

react-native-sound

But can not get it to work properly, I get this error: Cannot read property IsAndroid of undefined, which it tries to do in sound.js line 4 of the react-native-sound package:

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

Probably this means that the RNSound object is not registered correctly.

Anyways, it seems the package is not being maintained anymore with 33 issues and 8 pull request, and last mit was 4 months ago.

So, how do you guys add audio to your projects? I am using React Native 0.37 btw.

I am trying really hard to play an audio file from my react-native app. Currenty I am trying to use this package:

react-native-sound

But can not get it to work properly, I get this error: Cannot read property IsAndroid of undefined, which it tries to do in sound.js line 4 of the react-native-sound package:

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

Probably this means that the RNSound object is not registered correctly.

Anyways, it seems the package is not being maintained anymore with 33 issues and 8 pull request, and last mit was 4 months ago.

So, how do you guys add audio to your projects? I am using React Native 0.37 btw.

Share Improve this question asked Nov 22, 2016 at 1:40 abooayoobabooayoob 912 silver badges7 bronze badges 2
  • This may help github./zmxv/react-native-sound/issues/36 – Nirav Ranpara Commented Nov 22, 2016 at 1:43
  • Thanks, that thread helped alot! I will write an answer to my own question, based on that thread. – abooayoob Commented Nov 23, 2016 at 13:03
Add a ment  | 

1 Answer 1

Reset to default 4

Ok, so based on Nirav Ranpara's ment and the link he provided:
https://github./zmxv/react-native-sound/issues/36

These are the stepsto take to make it work in android using React Native 0.37

1. Edit android/settings.gradle to declare the project directory:

include ':RNSound', ':app'
project(':RNSound').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-sound/android')

2. Edit android/app/build.gradle to declare the project dependency:

   dependencies {
     ...
     pile project(':RNSound')
   }

3. Edit android/app/src/main/java/.../MainApplication.java to register the native module:

NOTE: MainApplication.java not MainActivity.java

...
import .zmxv.RNSound.RNSoundPackage;
...

...
@Override
protected List<ReactPackage> getPackages() {
  return Arrays.<ReactPackage>asList(
      new MainReactPackage(), // don't forget your ma
      new RNSoundPackage() // insert this line
  );
}
...

4. Import in your application like this:

import { default as Sound } from 'react-native-sound';

NOTE don't do this:

// wrong
var Sound = require('react-native-sound');

// also wrong
import {Sound} from 'react-native-sound';

NOTE: Step 1 and 2 are same as the documentation, while 3 and 4 differ.

I am trying really hard to play an audio file from my react-native app. Currenty I am trying to use this package:

react-native-sound

But can not get it to work properly, I get this error: Cannot read property IsAndroid of undefined, which it tries to do in sound.js line 4 of the react-native-sound package:

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

Probably this means that the RNSound object is not registered correctly.

Anyways, it seems the package is not being maintained anymore with 33 issues and 8 pull request, and last mit was 4 months ago.

So, how do you guys add audio to your projects? I am using React Native 0.37 btw.

I am trying really hard to play an audio file from my react-native app. Currenty I am trying to use this package:

react-native-sound

But can not get it to work properly, I get this error: Cannot read property IsAndroid of undefined, which it tries to do in sound.js line 4 of the react-native-sound package:

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

Probably this means that the RNSound object is not registered correctly.

Anyways, it seems the package is not being maintained anymore with 33 issues and 8 pull request, and last mit was 4 months ago.

So, how do you guys add audio to your projects? I am using React Native 0.37 btw.

Share Improve this question asked Nov 22, 2016 at 1:40 abooayoobabooayoob 912 silver badges7 bronze badges 2
  • This may help github./zmxv/react-native-sound/issues/36 – Nirav Ranpara Commented Nov 22, 2016 at 1:43
  • Thanks, that thread helped alot! I will write an answer to my own question, based on that thread. – abooayoob Commented Nov 23, 2016 at 13:03
Add a ment  | 

1 Answer 1

Reset to default 4

Ok, so based on Nirav Ranpara's ment and the link he provided:
https://github./zmxv/react-native-sound/issues/36

These are the stepsto take to make it work in android using React Native 0.37

1. Edit android/settings.gradle to declare the project directory:

include ':RNSound', ':app'
project(':RNSound').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-sound/android')

2. Edit android/app/build.gradle to declare the project dependency:

   dependencies {
     ...
     pile project(':RNSound')
   }

3. Edit android/app/src/main/java/.../MainApplication.java to register the native module:

NOTE: MainApplication.java not MainActivity.java

...
import .zmxv.RNSound.RNSoundPackage;
...

...
@Override
protected List<ReactPackage> getPackages() {
  return Arrays.<ReactPackage>asList(
      new MainReactPackage(), // don't forget your ma
      new RNSoundPackage() // insert this line
  );
}
...

4. Import in your application like this:

import { default as Sound } from 'react-native-sound';

NOTE don't do this:

// wrong
var Sound = require('react-native-sound');

// also wrong
import {Sound} from 'react-native-sound';

NOTE: Step 1 and 2 are same as the documentation, while 3 and 4 differ.

本文标签: javascriptHow to play audio file in reactnative Primarily in androidStack Overflow