$ yarn add react-native-volume-listener
$ react-native link react-native-volume-listener
- In XCode, in the project navigator, right click
Libraries
➜Add Files to [your project's name]
- Go to
node_modules
➜react-native-volume-listener
and addRNVolumeListen.xcodeproj
- In XCode, in the project navigator, select your project. Add
libRNVolumeListen.a
to your project'sBuild Phases
➜Link Binary With Libraries
- Run your project (
Cmd+R
)<
- Open up
android/app/src/main/java/[...]/MainApplication.java
- Add
import com.nghinv.volumelisten.RNVolumeListenPackage;
to the imports at the top of the file - Add
new RNVolumeListenPackage()
to the list returned by thegetPackages()
method
-
Append the following lines to
android/settings.gradle
:include ':react-native-volume-listener' project(':react-native-volume-listener').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-volume-listener/android')
-
Insert the following lines inside the dependencies block in
android/app/build.gradle
:compile project(':react-native-volume-listener')
-
Important
- Add
import android.view.KeyEvent;
andimport com.nghinv.volumelisten.RNVolumeListenModule;
to the imports at top of the file MainActivity.java - Append the following lines to MainActivity class
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { return RNVolumeListenModule.onKeyDownEvent(keyCode, event); }
import React, { Component } from 'react';
import { View } from 'react-native';
import VolumeListen from 'react-native-volume-listener';
// TODO: What to do with the module?
export default class Example extends Component {
onVolumePress = (volume) => {
// return UP or DOWN
console.log('onVolumePress', volume)
}
onChangeVolume = (volume) => {
//return current volume (0 -> 1)
console.log('onChangeVolume', volume)
}
render() {
return (
<View style={{flex: 1}}>
<VolumeListen
onVolumePress={this.onVolumePress}
onChangeVolume={this.onChangeVolume} // Only IOS
/>
</View>
)
}
}