react-native-android-volume-control
v0.1.2
Published
This package provide the possibility to enable and disable the volume control behavior on android.
Downloads
99
Readme
react-native-android-volume-control
React-native bridge which enable and disable volume control behavior on android.
Installation
npm install react-native-android-volume-control
or
yarn add react-native-android-volume-control
In your MainActivity.java
add :
@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
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 to learn how to contribute to the repository and the development workflow.
License
MIT