Skip to content

Commit

Permalink
[FIX] c19354837#134 addVolumeListener is not triggered on iOS 15
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor-Tiukavkin committed Nov 3, 2021
1 parent 7fd99df commit 9729eb2
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions ios/RTCSystemSetting.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <ifaddrs.h>
#import <net/if.h>
#import <AVFoundation/AVFoundation.h>

#ifdef BLUETOOTH
#import <CoreBluetooth/CoreBluetooth.h>
Expand All @@ -37,13 +38,14 @@ @implementation RCTSystemSetting {
UISlider *volumeSlider;
}

-(void)dealloc {
[self removeVolumeListener];
}

-(instancetype)init{
self = [super init];
if(self){
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(volumeChanged:)
name:@"AVSystemController_SystemVolumeDidChangeNotification"
object:nil];
[self addVolumeListener];
#ifdef BLUETOOTH
cb = [[CBCentralManager alloc] initWithDelegate:nil queue:nil options:@{CBCentralManagerOptionShowPowerAlertKey: @NO}];
#endif
Expand Down Expand Up @@ -218,13 +220,31 @@ -(void)stopObserving {
hasListeners = NO;
}

-(void)volumeChanged:(NSNotification *)notification{
if(skipSetVolumeCount == 0 && hasListeners){
float volume = [[[notification userInfo] objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"] floatValue];
[self sendEventWithName:@"EventVolume" body:@{@"value": [NSNumber numberWithFloat:volume]}];
}
if(skipSetVolumeCount > 0){
skipSetVolumeCount--;
- (void)addVolumeListener {
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:YES error:nil];
[audioSession addObserver:self
forKeyPath:@"outputVolume"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context:nil];
}

-(void)removeVolumeListener {
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
[audioSession removeObserver:self forKeyPath:@"outputVolume"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {

if (object == [AVAudioSession sharedInstance] && [keyPath isEqualToString:@"outputVolume"]) {
float newValue = [change[@"new"] floatValue];
if (skipSetVolumeCount == 0 && hasListeners) {
[self sendEventWithName:@"EventVolume"
body:@{@"value": [NSNumber numberWithFloat:newValue]}];
}
if (skipSetVolumeCount > 0) {
skipSetVolumeCount--;
}
}
}

Expand Down

0 comments on commit 9729eb2

Please sign in to comment.