# react-native-android-volume-control

React-native bridge which enable and disable volume control behavior on android.

## Installation

```sh
npm install react-native-android-volume-control
``` 
or
```sh
yarn add react-native-android-volume-control
``` 

In your ``MainActivity.java`` add :

```java
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
      AndroidVolumeControlModule androidVolumeControlModule = AndroidVolumeControlModule.Factory.getInstance();

      if (androidVolumeControlModule.onKeyDownEvent(keyCode, event)) {
          return true;
      }
      return super.onKeyDown(keyCode, event);
  }
```

## Usage

```js
import AndroidVolumeControlModule from 'react-native-android-volume-control';

// ...

const isIos = false; // <--- set your own flag
const [isActivated, setIsActivated] = useState(false);

useEffect(() => {
  if (isIos) return undefined;
  
  const androidVolumeControlEventEmitter = new NativeEventEmitter(AndroidVolumeControl);
  const keyDownListener = androidVolumeControlEventEmitter.addListener('onKeyDown', ({keyCode}: {keyCode: number}) => {
    if (keyCode === 24) yourComponentRef.action(); // <--- Call your own function here
    if (keyCode === 25) yourComponentRef.action(); // <--- Call your own function here
  });

  return () => keyDownListener.remove();
  
}, [yourComponentRef]);

useEffect(() => {
  if (isIos) return undefined;
  
  AndroidVolumeControl.setIsActivated(isActivated);

  return () => AndroidVolumeControl.setIsActivated(false); // <--- disable the feature when component unmount
}, [isActivated]);


```

## Contributing

See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.

## License

MIT
