From a93e6a4cb922c186e5fed2131018014f8dd3b965 Mon Sep 17 00:00:00 2001 From: zfir Date: Fri, 4 Nov 2022 15:33:17 +0400 Subject: [PATCH 1/5] if (_volumeView) { --- JPSVolumeButtonHandler/JPSVolumeButtonHandler.m | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/JPSVolumeButtonHandler/JPSVolumeButtonHandler.m b/JPSVolumeButtonHandler/JPSVolumeButtonHandler.m index caff513..3b60eb4 100644 --- a/JPSVolumeButtonHandler/JPSVolumeButtonHandler.m +++ b/JPSVolumeButtonHandler/JPSVolumeButtonHandler.m @@ -172,11 +172,15 @@ - (void)setInitialVolume { if (self.initialVolume > maxVolume) { self.initialVolume = maxVolume; self.isAdjustingInitialVolume = YES; - [self setSystemVolume:self.initialVolume]; + if (_volumeView) { + [self setSystemVolume:self.initialVolume]; + } } else if (self.initialVolume < minVolume) { self.initialVolume = minVolume; self.isAdjustingInitialVolume = YES; - [self setSystemVolume:self.initialVolume]; + if (_volumeView) { + [self setSystemVolume:self.initialVolume]; + } } } @@ -245,7 +249,9 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N } // Reset volume - [self setSystemVolume:self.initialVolume]; + if (_volumeView) { + [self setSystemVolume:self.initialVolume]; + } } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } From dfab5425fc81d2d591f3506e61a2133f2f5b2b1a Mon Sep 17 00:00:00 2001 From: zfir Date: Fri, 4 Nov 2022 18:39:01 +0400 Subject: [PATCH 2/5] CherPake Changes --- .../JPSVolumeButtonHandler.h | 1 + .../JPSVolumeButtonHandler.m | 35 ++++++++++++------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/JPSVolumeButtonHandler/JPSVolumeButtonHandler.h b/JPSVolumeButtonHandler/JPSVolumeButtonHandler.h index 81c9a10..acefc8d 100644 --- a/JPSVolumeButtonHandler/JPSVolumeButtonHandler.h +++ b/JPSVolumeButtonHandler/JPSVolumeButtonHandler.h @@ -23,6 +23,7 @@ typedef void (^JPSVolumeButtonBlock)(void); @property (nonatomic, strong) NSString * sessionCategory; @property (nonatomic, assign) AVAudioSessionCategoryOptions sessionOptions; +@property (nonatomic, strong) AVAudioSession * session; - (void)startHandler:(BOOL)disableSystemVolumeHandler; - (void)stopHandler; diff --git a/JPSVolumeButtonHandler/JPSVolumeButtonHandler.m b/JPSVolumeButtonHandler/JPSVolumeButtonHandler.m index 3b60eb4..8ceeb1d 100644 --- a/JPSVolumeButtonHandler/JPSVolumeButtonHandler.m +++ b/JPSVolumeButtonHandler/JPSVolumeButtonHandler.m @@ -12,15 +12,17 @@ // Comment/uncomment out NSLog to enable/disable logging #define JPSLog(fmt, ...) //NSLog(fmt, __VA_ARGS__) +#define volumeStep 0.06250f + static NSString *const sessionVolumeKeyPath = @"outputVolume"; static void *sessionContext = &sessionContext; -static CGFloat maxVolume = 0.99999f; -static CGFloat minVolume = 0.00001f; +static CGFloat maxVolume = 0.99999f - volumeStep; +static CGFloat minVolume = 0.00001f + volumeStep; @interface JPSVolumeButtonHandler () +@property (nonatomic, assign) CGFloat realVolume; @property (nonatomic, assign) CGFloat initialVolume; -@property (nonatomic, strong) AVAudioSession * session; @property (nonatomic, strong) MPVolumeView * volumeView; @property (nonatomic, assign) BOOL appIsActive; @property (nonatomic, assign) BOOL isStarted; @@ -106,12 +108,12 @@ - (void)setupSession { withOptions:_sessionOptions error:&error]; if (error) { - NSLog(@"%@", error); + JPSLog(@"%@", error); return; } [self.session setActive:YES error:&error]; if (error) { - NSLog(@"%@", error); + JPSLog(@"%@", error); return; } @@ -157,7 +159,7 @@ - (void)audioSessionInterrupted:(NSNotification*)notification { NSError *error = nil; [self.session setActive:YES error:&error]; if (error) { - NSLog(@"%@", error); + JPSLog(@"%@", error); } break; } @@ -168,7 +170,8 @@ - (void)audioSessionInterrupted:(NSNotification*)notification { } - (void)setInitialVolume { - self.initialVolume = self.session.outputVolume; + self.realVolume = self.session.outputVolume; + self.initialVolume = self.realVolume; if (self.initialVolume > maxVolume) { self.initialVolume = maxVolume; self.isAdjustingInitialVolume = YES; @@ -186,8 +189,11 @@ - (void)setInitialVolume { - (void)applicationDidChangeActive:(NSNotification *)notification { self.appIsActive = [notification.name isEqualToString:UIApplicationDidBecomeActiveNotification]; - if (self.appIsActive && self.isStarted) { + if (!self.isStarted) return; + if (self.appIsActive ) { [self setInitialVolume]; + } else { + [self setSystemVolume:self.realVolume]; } } @@ -206,13 +212,14 @@ + (instancetype)volumeButtonHandlerWithUpBlock:(JPSVolumeButtonBlock)upBlock dow - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (context == sessionContext) { + CGFloat oldVolume = [change[NSKeyValueChangeOldKey] floatValue]; + CGFloat newVolume = [change[NSKeyValueChangeNewKey] floatValue]; + JPSLog(@"Volume change detected: %f -> %f", oldVolume, newVolume); + if (!self.appIsActive) { // Probably control center, skip blocks return; } - - CGFloat newVolume = [change[NSKeyValueChangeNewKey] floatValue]; - CGFloat oldVolume = [change[NSKeyValueChangeOldKey] floatValue]; if (self.disableSystemVolumeHandler && newVolume == self.initialVolume) { // Resetting volume, skip blocks @@ -252,6 +259,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N if (_volumeView) { [self setSystemVolume:self.initialVolume]; } + JPSLog(@"Restoring volume to %f (actual: %f)", self.initialVolume, self.session.outputVolume); } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } @@ -262,7 +270,10 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N - (void)setSystemVolume:(CGFloat)volume { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [[MPMusicPlayerController applicationMusicPlayer] setVolume:(float)volume]; + dispatch_async(dispatch_get_main_queue(), ^{ + [[MPMusicPlayerController applicationMusicPlayer] setVolume:(float)volume]; + JPSLog(@"Changed volume to %f (actual: %f)", volume, self.session.outputVolume); + }); #pragma clang diagnostic pop } From ddd2149ca48914082e7718bb0b1a54b9002ef9c3 Mon Sep 17 00:00:00 2001 From: zfir Date: Fri, 4 Nov 2022 19:32:59 +0400 Subject: [PATCH 3/5] if (_volumeView) { --- JPSVolumeButtonHandler/JPSVolumeButtonHandler.m | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/JPSVolumeButtonHandler/JPSVolumeButtonHandler.m b/JPSVolumeButtonHandler/JPSVolumeButtonHandler.m index 8ceeb1d..912dfe8 100644 --- a/JPSVolumeButtonHandler/JPSVolumeButtonHandler.m +++ b/JPSVolumeButtonHandler/JPSVolumeButtonHandler.m @@ -193,7 +193,9 @@ - (void)applicationDidChangeActive:(NSNotification *)notification { if (self.appIsActive ) { [self setInitialVolume]; } else { - [self setSystemVolume:self.realVolume]; + if (_volumeView) { + [self setSystemVolume:self.realVolume]; + } } } From db85f0560d1599ab18fae5a75a419f611cfe85f7 Mon Sep 17 00:00:00 2001 From: zfir Date: Sat, 5 Nov 2022 12:10:18 +0400 Subject: [PATCH 4/5] Remove realVolume Variable --- JPSVolumeButtonHandler/JPSVolumeButtonHandler.m | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/JPSVolumeButtonHandler/JPSVolumeButtonHandler.m b/JPSVolumeButtonHandler/JPSVolumeButtonHandler.m index 912dfe8..84e2ab0 100644 --- a/JPSVolumeButtonHandler/JPSVolumeButtonHandler.m +++ b/JPSVolumeButtonHandler/JPSVolumeButtonHandler.m @@ -21,7 +21,6 @@ @interface JPSVolumeButtonHandler () -@property (nonatomic, assign) CGFloat realVolume; @property (nonatomic, assign) CGFloat initialVolume; @property (nonatomic, strong) MPVolumeView * volumeView; @property (nonatomic, assign) BOOL appIsActive; @@ -170,8 +169,7 @@ - (void)audioSessionInterrupted:(NSNotification*)notification { } - (void)setInitialVolume { - self.realVolume = self.session.outputVolume; - self.initialVolume = self.realVolume; + self.initialVolume = self.session.outputVolume; if (self.initialVolume > maxVolume) { self.initialVolume = maxVolume; self.isAdjustingInitialVolume = YES; @@ -192,10 +190,6 @@ - (void)applicationDidChangeActive:(NSNotification *)notification { if (!self.isStarted) return; if (self.appIsActive ) { [self setInitialVolume]; - } else { - if (_volumeView) { - [self setSystemVolume:self.realVolume]; - } } } From ea9024809a1a8f769828eb6bfc4343d71ba8c736 Mon Sep 17 00:00:00 2001 From: parveshneedhoo Date: Tue, 18 Jul 2023 11:18:46 +0400 Subject: [PATCH 5/5] return when setting initial volume --- JPSVolumeButtonHandler/JPSVolumeButtonHandler.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/JPSVolumeButtonHandler/JPSVolumeButtonHandler.m b/JPSVolumeButtonHandler/JPSVolumeButtonHandler.m index 84e2ab0..1ccb5c1 100644 --- a/JPSVolumeButtonHandler/JPSVolumeButtonHandler.m +++ b/JPSVolumeButtonHandler/JPSVolumeButtonHandler.m @@ -188,6 +188,7 @@ - (void)setInitialVolume { - (void)applicationDidChangeActive:(NSNotification *)notification { self.appIsActive = [notification.name isEqualToString:UIApplicationDidBecomeActiveNotification]; if (!self.isStarted) return; + if (!self.isAdjustingInitialVolume) return; if (self.appIsActive ) { [self setInitialVolume]; } @@ -226,6 +227,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N return; } self.isAdjustingInitialVolume = NO; + return; } CGFloat difference = fabs(newVolume-oldVolume);