20

I am trying to detect when the user presses volume up button within a react-native application. I am not using any media as music or video directly in the phone, I want to use the volume buttons for a different purpose. e.g: triggering the camera shot.

jsdario
  • 6,477
  • 7
  • 41
  • 75
  • 2
    I need exactly the same thing and don't know why they put your question on hold !!! please let me know if you have found any solution thanks indeed – farmcommand2 Dec 17 '17 at 01:54
  • 1
    Just rephrased it not to ask for a library recommendation. I just described the problem. This questions does not lead to opinionated answers, so I guess the off-topic label should be taken off. – jsdario Feb 23 '18 at 10:01
  • 3
    This question clearly adds value for the community but is still closed as off-topic, preventing people from answering it – jsdario Feb 04 '19 at 10:51
  • @jsdario did you find any solution? – David Mar 15 '19 at 12:04
  • Sadly not - I moved on to other projects – jsdario May 22 '19 at 16:12

3 Answers3

1

@dv3's answer is helpful for iOS, but IFours' package will not work on Android (see https://github.com/ifours/react-native-volume-slider/issues/1).

My solution for Android has been to use https://www.npmjs.com/package/react-native-volume. This will detect media volume changes. (I'm pretty new to React Native so I'm still working on a way to force the volume button to change the media volume specifically).

Edit: playing a silent sound on loop to force the "media" volume input. Pretty hacky but it works.

gwitwer
  • 11
  • 2
  • 3
  • So I need other package to play some media and listen for physical butotn presses? Your hack seems pretty interesting, but it would affect users memory and battery, does it? – jsdario Jul 31 '17 at 09:00
  • The main drain on battery life is screen usage. Intuition tells me decibel level output will also affect battery life (but I really don't know). However if the screen is off and the sound is silent, I don't think the battery will drain *too* quickly. Although you're right -- it's definitely not an ideal solution. – gwitwer Aug 03 '17 at 15:25
1

This looks promising: https://github.com/c19354837/react-native-system-setting

Also for getting Brightness, Wifi switch, Location, Bluetooth status

Works on iOS & Android

Evalds Urtans
  • 6,436
  • 1
  • 41
  • 31
  • Yeah it has volume listeners so this is probably what I need, I'll test it out and come back. – jsdario Feb 23 '18 at 10:02
  • 1
    I've tried it. Unfortunately, it doesn't work. – Dmitry Stepanov Mar 11 '19 at 11:42
  • **Volume Listener** handle event when system volume is changed. After first hit on volume button volume is not change. It's just opening system slider. After second hit on this button you can to handle event on this listener. But system slider will shown. – Dmitry Stepanov Mar 11 '19 at 11:55
0

I would suggest that you make your own component. If you want an existing module: implement it with https://github.com/IFours/react-native-volume-slider by making it invisible and listen to onValueChange.

dv3
  • 4,369
  • 5
  • 28
  • 50