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

JPSVolumeButtonHandler Added feature to allow the handler to only res… #45

Merged
merged 1 commit into from
Apr 19, 2017
Merged
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
4 changes: 4 additions & 0 deletions JPSVolumeButtonHandler/JPSVolumeButtonHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ typedef void (^JPSVolumeButtonBlock)();
- (void)startHandler:(BOOL)disableSystemVolumeHandler;
- (void)stopHandler;

// A Function to set exactJumpsOnly. When set to YES, only volume jumps of .0625 call the code blocks.
// If it doesn't match, the code blocks are not called and setInitialVolume is called
- (void)useExactJumpsOnly:(BOOL)enabled;

// Returns a button handler with the specified up/down volume button blocks
+ (instancetype)volumeButtonHandlerWithUpBlock:(JPSVolumeButtonBlock)upBlock downBlock:(JPSVolumeButtonBlock)downBlock;

Expand Down
25 changes: 22 additions & 3 deletions JPSVolumeButtonHandler/JPSVolumeButtonHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

// Comment/uncomment out NSLog to enable/disable logging
#define JPSLog(fmt, ...) //NSLog(fmt, __VA_ARGS__)

static NSString *const sessionVolumeKeyPath = @"outputVolume";
static void *sessionContext = &sessionContext;
static CGFloat maxVolume = 0.99999f;
Expand All @@ -24,6 +27,7 @@ @interface JPSVolumeButtonHandler ()
@property (nonatomic, assign) BOOL isStarted;
@property (nonatomic, assign) BOOL disableSystemVolumeHandler;
@property (nonatomic, assign) BOOL isAdjustingInitialVolume;
@property (nonatomic, assign) BOOL exactJumpsOnly;

@end

Expand All @@ -43,6 +47,8 @@ - (id)init {
[[UIApplication sharedApplication].windows.firstObject addSubview:_volumeView];

_volumeView.hidden = YES;

_exactJumpsOnly = NO;
}
return self;
}
Expand Down Expand Up @@ -130,16 +136,20 @@ - (void)setupSession {
self.volumeView.hidden = !self.disableSystemVolumeHandler;
}

- (void) useExactJumpsOnly:(BOOL)enabled{
_exactJumpsOnly = enabled;
}

- (void)audioSessionInterrupted:(NSNotification*)notification {
NSDictionary *interuptionDict = notification.userInfo;
NSInteger interuptionType = [[interuptionDict valueForKey:AVAudioSessionInterruptionTypeKey] integerValue];
switch (interuptionType) {
case AVAudioSessionInterruptionTypeBegan:
// NSLog(@"Audio Session Interruption case started.");
JPSLog(@"Audio Session Interruption case started.", nil);
break;
case AVAudioSessionInterruptionTypeEnded:
{
// NSLog(@"Audio Session Interruption case ended.");
JPSLog(@"Audio Session Interruption case ended.", nil);
NSError *error = nil;
[self.session setActive:YES error:&error];
if (error) {
Expand All @@ -148,7 +158,7 @@ - (void)audioSessionInterrupted:(NSNotification*)notification {
break;
}
default:
// NSLog(@"Audio Session Interruption Notification case default.");
JPSLog(@"Audio Session Interruption Notification case default.", nil);
break;
}
}
Expand Down Expand Up @@ -206,6 +216,15 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
}
self.isAdjustingInitialVolume = NO;
}

CGFloat difference = fabs(newVolume-oldVolume);

JPSLog(@"Old Vol:%f New Vol:%f Difference = %f", (double)oldVolume, (double)newVolume, (double) difference);
if (_exactJumpsOnly && (difference > .063 || difference < .062)) {
JPSLog(@"Ignoring non-standard Jump of %f, which is not the .0625 a press of the actually volume button would have resulted in.", difference);
[self setInitialVolume];
return;
}

if (newVolume > oldVolume) {
if (self.upBlock) self.upBlock();
Expand Down