react-native-system-setting icon indicating copy to clipboard operation
react-native-system-setting copied to clipboard

`addVolumeListener` is fired twice when press volume key

Open heyman333 opened this issue 7 years ago • 6 comments

I just tested android device only. There is a problem that the addVolumeListener is called twice when the sound mode is "music".

my code snipppet is here


  componentDidMount = () => {
    this.volumeListener = SystemSetting.addVolumeListener(this.setVolume);
  };

  componentWillUnmount = () => {
    SystemSetting.removeVolumeListener(this.volumeListener);
  };

  setVolume = currentVolume => {
    console.log("call setvolume");
    this.setState({ currentVolume: currentVolume.value });
  };

heyman333 avatar Oct 15 '18 07:10 heyman333

Thanks for your feedback!

I reproduce it in my android phone. If I block keyup event for volume button in onKeyUp(), it'll be called once only. So I think twice calling is for keydown and keyup. I am not sure if this is a feature of android.

c19354837 avatar Oct 16 '18 16:10 c19354837

@c19354837 ping any news on this please ?

MaximeBrl avatar Nov 19 '18 15:11 MaximeBrl

hello @MaximeBrl I recommend you to use shouldComponentUpdate or implementing React.PureComponent to block unnecessary re-rendering

heyman333 avatar Nov 24 '18 05:11 heyman333

Thank you, I'll test it tomorrow !

MaximeBrl avatar Dec 11 '18 18:12 MaximeBrl

This solution does not work for my problem. I start the camera, thanks to a button, a self-timer starts, then after the recording starts. The problem with shouldComponentUpdate is that I don't care about the rendering of the component. Thanks to the VolumeListener, I want to be able to start recording with the volume button. With a multiple call from the listener, it prevents me from doing what I want and the shouldComponentUpdate() doesn't work because it only concerns rendering.

Sorry I'm French and I must have made some mistakes.

Edit : I have found a solution but it's not a good solution

MaximeBrl avatar Dec 13 '18 13:12 MaximeBrl

Hi. Did anyone solve this issue?

dreamdev21 avatar Mar 18 '19 08:03 dreamdev21