1.在项目根目录下执行安装包下载
npm install react-native-sound --save2. 然后自动使用链接:
react-native link react-native-sound3.我自己是执行完以上两个步骤后查看了一下目录中相对的js配置文档,都是自动配置好的,但是以防意外,还是建议需要再核对一下
编辑android/settings.gradle
以声明项目目录:
include ':react-native-sound'
project(':react-native-sound').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-sound/android')
编辑android/app/build.gradle
以声明项目依赖关系:
dependencies {
...
compile project(':react-native-sound')
}
编辑android/app/src/main/java/.../MainApplication.java
注册本机模块:
...
import com.zmxv.RNSound.RNSoundPackage ; // < - New
...
public class MainApplication extends Application implements ReactApplication {
...
@Override
protected List < ReactPackage > getPackages(){
return Arrays 。< ReactPackage > asList(
new MainReactPackage(),
new RNSoundPackage()// < - New
);
}
注意:官方文档说明如果是旧版本的需要再更改一下
对于旧版本的React Native,您需要修改MainActivity.java
:
...
import com.zmxv.RNSound.RNSoundPackage ; // < - New
...
public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
...
@Override
protected void onCreate(Bundle savedInstanceState){
...
mReactInstanceManager = ReactInstanceManager 。生成器()
.setApplication(getApplication())
...
.addPackage(new MainReactPackage())
.addPackage(new RNSoundPackage())// < - New
...
}
另官方的文档还有一项说明:
android/app/src/main/res/raw
. Note that files in this directory must be lowercase and underscored (e.g. my_file_name.mp3) and that subdirectories are not supported by Android.执行效果如图:
index.android.js的文件原码
原项目git仓库地址:https://github.com/zmxv/react-native-sound