Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to only listen to some volume change notification reasons #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Unrealeased
Allow users to only listen to some volume change notification reasons
# V1.7.6
**2020-10-11**

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions SystemSetting.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ interface SystemSetting {
restoreBrightness: () => number;
getVolume: (type?: VolumeType) => Promise<number>;
setVolume: (value: number, config?: VolumeConfig | VolumeType) => void;
setVolumeChangeListenerReasons: (val: Array<string>) => Promise<boolean>;
addVolumeListener: (
callback: (volumeData: VolumeData) => void
) => EmitterSubscription;
Expand Down
4 changes: 4 additions & 0 deletions SystemSetting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions examples/SystemSettingExample/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 13 additions & 2 deletions ios/RTCSystemSetting.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ @interface RCTSystemSetting()

@implementation RCTSystemSetting {
bool hasListeners;
NSArray<NSString *> * _Nonnull volumeChangeListenerReasons;
long skipSetVolumeCount;
#ifdef BLUETOOTH
CBCentralManager *cb;
Expand All @@ -53,6 +54,7 @@ -(instancetype)init{
#ifdef PRIVATE_API
[self initSetting];
#endif
volumeChangeListenerReasons = @[];

return self;
}
Expand Down Expand Up @@ -97,6 +99,11 @@ +(BOOL)requiresMainQueueSetup{
resolve([NSNumber numberWithDouble:[UIScreen mainScreen].brightness]);
}

RCT_EXPORT_METHOD(setVolumeChangeListenerReasons:(nonnull NSArray<NSString *> *)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(), ^{
Expand Down Expand Up @@ -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--;
Expand Down