react-native-volume-control
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

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

  1. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
  2. Go to node_modulesreact-native-volume-control => ios
    • add ReactNativeVolumeControl.xcodeproj to the Libraries folder in your XCode project
  3. In XCode, in the project navigator, select your project. Add libReactNativeVolumeControl.a to your project's Build PhasesLink Binary With Libraries
  4. Run your project (Cmd+R)

Android

Manual installation

  1. In Android Studio open Module Settings and add a Gradle Project.
  2. Look for react-native-volume-control android folder and link with a Gradle.
  3. 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
      />
    )
  }
}

Readme

Keywords

Package Sidebar

Install

npm i react-native-volume-control

Weekly Downloads

49

Version

1.0.1

License

ISC

Unpacked Size

753 kB

Total Files

110

Last publish

Collaborators

  • rtmalone