hirbod / react-native-volume-manager

React Native module which adds the ability to change the system volume on iOS and Android, listen to volume changes and supress the native volume UI to build your own volume slider or UX. It can listen to iOS mute switch and ringer mode changes on Android (and let you set the ringer mode)
MIT License
216 stars 14 forks source link

listen volume changes when the app is background #22

Closed Nadimkhan120 closed 7 months ago

Nadimkhan120 commented 7 months ago

hi @hirbod can we listen volume changes when the app is background or quit state?

Nasseratic commented 7 months ago

Hi, I would want Hirbod to confirm. But in theory, when the app is backgrounded the normal app js won't run, you should not be able to unless you use some sort of background task to do that.

What is your use case?

hirbod commented 7 months ago

@Nasseratic, your assumption is correct. React Native Apps that have been sent to the background might continue to work for 60 seconds up to three minutes. Thus, events can be received while the app is backgrounded, but only for a very short period. Typically, music sessions are also lost when the app is in the background (unless a background music service has been registered). Therefore, there isn't a consistent method, and I believe this is beyond the scope of this package.

suseendhar commented 6 months ago

useEffect(() => { const volumeListener = VolumeManager.addVolumeListener(result => { console.log('Volume changed:', result); });

return () => {
  volumeListener.remove();
};

}, []);

@hirbod @Nasseratic addVolumeListener is not triggering when screen locks and resume back ... the addVolumeListener wont trigger if we screen locks and resume?

hirbod commented 6 months ago

@suseendhar check this issue for a solution. The main reason is that the audio session is lost:

https://github.com/hirbod/react-native-volume-manager/issues/21

suseendhar commented 6 months ago

@hirbod Thanks after adding this line VolumeManager.enable(true) in app state active it is working