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

0.1.2 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i react-native-android-volume-control

Weekly Downloads

12

Version

0.1.2

License

MIT

Unpacked Size

262 kB

Total Files

93

Last publish

Collaborators

  • vivlio