Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Volume button bug on start #68

Open
wants to merge 8 commits 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
1 change: 1 addition & 0 deletions JPSVolumeButtonHandler/JPSVolumeButtonHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
43 changes: 29 additions & 14 deletions JPSVolumeButtonHandler/JPSVolumeButtonHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
// 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 initialVolume;
@property (nonatomic, strong) AVAudioSession * session;
@property (nonatomic, strong) MPVolumeView * volumeView;
@property (nonatomic, assign) BOOL appIsActive;
@property (nonatomic, assign) BOOL isStarted;
Expand Down Expand Up @@ -106,12 +107,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;
}

Expand Down Expand Up @@ -157,7 +158,7 @@ - (void)audioSessionInterrupted:(NSNotification*)notification {
NSError *error = nil;
[self.session setActive:YES error:&error];
if (error) {
NSLog(@"%@", error);
JPSLog(@"%@", error);
}
break;
}
Expand All @@ -172,17 +173,23 @@ - (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];
}
}
}

- (void)applicationDidChangeActive:(NSNotification *)notification {
self.appIsActive = [notification.name isEqualToString:UIApplicationDidBecomeActiveNotification];
if (self.appIsActive && self.isStarted) {
if (!self.isStarted) return;
if (!self.isAdjustingInitialVolume) return;
if (self.appIsActive ) {
[self setInitialVolume];
}
}
Expand All @@ -202,13 +209,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
Expand All @@ -219,6 +227,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
return;
}
self.isAdjustingInitialVolume = NO;
return;
}

CGFloat difference = fabs(newVolume-oldVolume);
Expand All @@ -245,7 +254,10 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
}

// Reset volume
[self setSystemVolume:self.initialVolume];
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];
}
Expand All @@ -256,7 +268,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
}

Expand Down