4.0.0 • Published 6 years ago
@jvgaeta/react-native-volume-control v4.0.0
react-native-volume-control
Control device volume for iOS and Android.
First installation step (applied for both iOS & Android)
$ npm install react-native-volume-control --save
2. Automatic installation
$ react-native link react-native-volume-control
3. Manual installation
- In XCode, in the project navigator, right click
Libraries➜Add Files to [your project's name] - Go to
node_modules➜react-native-volume-control=>ios- add
ReactNativeVolumeControl.xcodeprojto the Libraries folder in your XCode project
- add
- In XCode, in the project navigator, select your project. Add
libReactNativeVolumeControl.ato your project'sBuild Phases➜Link Binary With Libraries - Run your project (
Cmd+R)
Android
Manual installation
- In Android Studio open
Module Settingsand add a Gradle Project. - Look for
react-native-volume-controlandroid folder and link with a Gradle. - Open MyApplication.java from main app and put the ReactNativeVolumeControllerPackage
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new RNVolumeControlPackage()
);
}Usage
This component only exposes an api to update device volume and listens for VolumeChanged events via hardware buttons. There is no UI component included.
// Other imports
...
import VolumeControl, {
VolumeControlEvents
} from "react-native-volume-control";
import Slider from '@react-native-community/slider';
class App extends React.Component {
state = {
volume: 0
}
async componentDidMount() {
this.setState({
volume: await VolumeControl.getVolume()
});
// Add and store event listener
this.volEvent = VolumeControlEvents.addListener(
"VolumeChanged",
this.volumeEvent
);
}
// Updates Slider UI when hardware buttons change volume
volumeEvent = event => {
this.setState({ volume: event.volume });
};
// Updates device volume
sliderChange(value) {
VolumeControl.change(value);
}
componentWillUnmount() {
// remove event listener
this.volEvent.remove();
}
render() {
return (
<Slider
value={this.state.volume}
onValueChange={this.sliderChange}
// Other props
/>
)
}
}