简体   繁体   English

如何在react-native中播放音频文件? 主要在android中

[英]How to play audio file in react-native? Primarily in android

I am trying really hard to play an audio file from my react-native app. 我正在努力从我的react-native应用程序播放音频文件。 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: 但是无法让它正常工作,我得到了这个错误:无法读取未定义的属性IsAndroid,它试图在react-native-sound包的sound.js第4行中执行:

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

Probably this means that the RNSound object is not registered correctly. 可能这意味着RNSound对象未正确注册。

Anyways, it seems the package is not being maintained anymore with 33 issues and 8 pull request, and last commit was 4 months ago. 无论如何,似乎包裹不再被维护33个问题和8个拉取请求,最后一次提交是4个月前。

So, how do you guys add audio to your projects? 那么,你们如何为你的项目添加音频? I am using React Native 0.37 btw. 我正在使用React Native 0.37顺便说一句。

Ok, so based on Nirav Ranpara's comment and the link he provided: 好的,基于Nirav Ranpara的评论和他提供的链接:
https://github.com/zmxv/react-native-sound/issues/36 https://github.com/zmxv/react-native-sound/issues/36

These are the stepsto take to make it work in android using React Native 0.37 这些是使用React Native 0.37使其在Android中运行的步骤

1. Edit android/settings.gradle to declare the project directory: 1.编辑android / settings.gradle以声明项目目录:

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: 2.编辑android / app / build.gradle以声明项目依赖项:

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

3. Edit android/app/src/main/java/.../MainApplication.java to register the native module: 3.编辑android / app / src / main / java /.../ MainApplication.java以注册本机模块:

NOTE: MainApplication.java not MainActivity.java 注意: MainApplication.java不是MainActivity.java

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

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

4. Import in your application like this: 4.在您的应用程序中导入如下:

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. 注意:步骤1和2与文档相同,而3和4不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM