From ac65c0dc741f40388dde792003bb5bcc4b010695 Mon Sep 17 00:00:00 2001 From: Pedro A Osuna Date: Mon, 21 Jun 2021 10:52:20 -0700 Subject: [PATCH] Allow users to only listen to some volume change notification reasons --- CHANGELOG.md | 2 ++ README.md | 5 +++++ SystemSetting.d.ts | 1 + SystemSetting.js | 4 ++++ examples/SystemSettingExample/App.js | 2 ++ ios/RTCSystemSetting.m | 15 +++++++++++++-- 6 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e1cac9..1c3c4f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +# Unrealeased +Allow users to only listen to some volume change notification reasons # V1.7.6 **2020-10-11** diff --git a/README.md b/README.md index 28ee5be..89c7317 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,11 @@ SystemSetting.getVolume().then((volume)=>{ // change the volume SystemSetting.setVolume(0.5); +// listen only to some volume changes, based on the change reason (AVSystemController_AudioVolumeChangeReasonNotificationParameter). +// If missing API call or sent an empty array it will listen to all changes. +// Pass reasons like "ExplicitVolumeChange", "RouteChange", et al +SystemSetting.setVolumeChangeListenerReasons(["ExplicitVolumeChange"]) + // listen the volume changing if you need const volumeListener = SystemSetting.addVolumeListener((data) => { const volume = data.value; diff --git a/SystemSetting.d.ts b/SystemSetting.d.ts index 82cb2e7..cf9cd7c 100644 --- a/SystemSetting.d.ts +++ b/SystemSetting.d.ts @@ -43,6 +43,7 @@ interface SystemSetting { restoreBrightness: () => number; getVolume: (type?: VolumeType) => Promise; setVolume: (value: number, config?: VolumeConfig | VolumeType) => void; + setVolumeChangeListenerReasons: (val: Array) => Promise; addVolumeListener: ( callback: (volumeData: VolumeData) => void ) => EmitterSubscription; diff --git a/SystemSetting.js b/SystemSetting.js index d6cdd11..0662959 100644 --- a/SystemSetting.js +++ b/SystemSetting.js @@ -128,6 +128,10 @@ export default class SystemSetting { SystemSettingNative.setVolume(val, config) } + static setVolumeChangeListenerReasons(val) { + return SystemSettingNative.setVolumeChangeListenerReasons(val) + } + static addVolumeListener(callback) { return eventEmitter.addListener('EventVolume', callback) } diff --git a/examples/SystemSettingExample/App.js b/examples/SystemSettingExample/App.js index bafbb9d..ba81b4c 100644 --- a/examples/SystemSettingExample/App.js +++ b/examples/SystemSettingExample/App.js @@ -46,6 +46,8 @@ export default class SystemSettingExample extends Component { this._changeSliderNativeVol(this.sliderVol, this.state.volume) this._changeSliderNativeVol(this.sliderBri, this.state.brightness) + SystemSetting.setVolumeChangeListenerReasons(["ExplicitVolumeChange"]) + this.volumeListener = SystemSetting.addVolumeListener((data) => { const volume = this.isAndroid ? data[this.state.volType] : data.value this._changeSliderNativeVol(this.sliderVol, volume) diff --git a/ios/RTCSystemSetting.m b/ios/RTCSystemSetting.m index e9fe63f..242ea69 100644 --- a/ios/RTCSystemSetting.m +++ b/ios/RTCSystemSetting.m @@ -28,6 +28,7 @@ @interface RCTSystemSetting() @implementation RCTSystemSetting { bool hasListeners; + NSArray * _Nonnull volumeChangeListenerReasons; long skipSetVolumeCount; #ifdef BLUETOOTH CBCentralManager *cb; @@ -53,6 +54,7 @@ -(instancetype)init{ #ifdef PRIVATE_API [self initSetting]; #endif + volumeChangeListenerReasons = @[]; return self; } @@ -97,6 +99,11 @@ +(BOOL)requiresMainQueueSetup{ resolve([NSNumber numberWithDouble:[UIScreen mainScreen].brightness]); } +RCT_EXPORT_METHOD(setVolumeChangeListenerReasons:(nonnull NSArray *)val resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){ + volumeChangeListenerReasons = val; + resolve([NSNumber numberWithBool:YES]); +} + RCT_EXPORT_METHOD(setVolume:(float)val config:(NSDictionary *)config){ skipSetVolumeCount++; dispatch_sync(dispatch_get_main_queue(), ^{ @@ -220,8 +227,12 @@ -(void)stopObserving { -(void)volumeChanged:(NSNotification *)notification{ if(skipSetVolumeCount == 0 && hasListeners){ - float volume = [[[notification userInfo] objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"] floatValue]; - [self sendEventWithName:@"EventVolume" body:@{@"value": [NSNumber numberWithFloat:volume]}]; + NSDictionary *userInfo = [notification userInfo]; + NSString *reason = [userInfo objectForKey:@"AVSystemController_AudioVolumeChangeReasonNotificationParameter"]; + if (volumeChangeListenerReasons.count == 0 || [volumeChangeListenerReasons containsObject:reason]) { + float volume = [[userInfo objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"] floatValue]; + [self sendEventWithName:@"EventVolume" body:@{@"value": [NSNumber numberWithFloat:volume]}]; + } } if(skipSetVolumeCount > 0){ skipSetVolumeCount--;