最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to play audio file in react-native? Primarily in android - Stack Overflow

matteradmin6PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far