Skip to content

Commit

Permalink
Merge pull request #1 from monkeydom/master
Browse files Browse the repository at this point in the history
rotation fix, changed ivars to autosynthesized properties, fixed iOS 7 flash of volume overlay
  • Loading branch information
Ricardo Naharro committed Dec 16, 2013
2 parents 890ada0 + e193a45 commit 8a7cd1c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 63 deletions.
12 changes: 2 additions & 10 deletions VolumeSnap/RBVolumeButtons.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,11 @@
typedef void (^ButtonBlock)();

@interface RBVolumeButtons : NSObject
{
float launchVolume;
BOOL hadToLowerVolume;
BOOL hadToRaiseVolume;

BOOL _isStealingVolumeButtons;
BOOL _suspended;
UIView *_volumeView;
}
{}

@property (nonatomic, copy) ButtonBlock upBlock;
@property (nonatomic, copy) ButtonBlock downBlock;
@property (readonly) float launchVolume;
@property (nonatomic, readonly) float launchVolume;

-(void)startStealingVolumeButtonEvents;
-(void)stopStealingVolumeButtonEvents;
Expand Down
83 changes: 46 additions & 37 deletions VolumeSnap/RBVolumeButtons.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,16 @@ -(void)stopStealingVolumeButtonEvents;

@property BOOL isStealingVolumeButtons;
@property BOOL suspended;
@property (strong) UIView *volumeView;
@property (nonatomic, strong) UIView *volumeView;
@property (nonatomic, readwrite) float launchVolume;

@property (nonatomic) BOOL hadToLowerVolume;
@property (nonatomic) BOOL hadToRaiseVolume;

@end

@implementation RBVolumeButtons

@synthesize upBlock;
@synthesize downBlock;
@synthesize launchVolume;
@synthesize isStealingVolumeButtons = _isStealingVolumeButtons;
@synthesize suspended = _suspended;
@synthesize volumeView = _volumeView;

void volumeListenerCallback (
void *inClientData,
AudioSessionPropertyID inID,
Expand Down Expand Up @@ -63,7 +60,7 @@ -(void)volumeDown
{
AudioSessionRemovePropertyListenerWithUserData(kAudioSessionProperty_CurrentHardwareOutputVolume, volumeListenerCallback, (__bridge void *)(self));

[[MPMusicPlayerController applicationMusicPlayer] setVolume:launchVolume];
[[MPMusicPlayerController applicationMusicPlayer] setVolume:self.launchVolume];

[self performSelector:@selector(initializeVolumeButtonStealer) withObject:self afterDelay:0.1];

Expand All @@ -78,7 +75,7 @@ -(void)volumeUp
{
AudioSessionRemovePropertyListenerWithUserData(kAudioSessionProperty_CurrentHardwareOutputVolume, volumeListenerCallback, (__bridge void *)(self));

[[MPMusicPlayerController applicationMusicPlayer] setVolume:launchVolume];
[[MPMusicPlayerController applicationMusicPlayer] setVolume:self.launchVolume];

[self performSelector:@selector(initializeVolumeButtonStealer) withObject:self afterDelay:0.1];

Expand All @@ -103,46 +100,58 @@ -(id)init

-(void)startStealingVolumeButtonEvents
{
NSAssert([[NSThread currentThread] isMainThread], @"This must be called from the main thread");
if(self.isStealingVolumeButtons) {
return;
}
NSAssert([[NSThread currentThread] isMainThread], @"This must be called from the main thread");
if(self.isStealingVolumeButtons) {
return;
}

self.isStealingVolumeButtons = YES;

AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionSetActive(YES);

launchVolume = [[MPMusicPlayerController applicationMusicPlayer] volume];
hadToLowerVolume = launchVolume == 1.0;
hadToRaiseVolume = launchVolume == 0.0;


AudioSessionInitialize(NULL, NULL, NULL, NULL);

UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;
AudioSessionSetProperty (
kAudioSessionProperty_AudioCategory,
sizeof (sessionCategory),
&sessionCategory
);

AudioSessionSetActive(YES);

CGRect frame = CGRectMake(0, -10, 1, 1);
self.volumeView = [[MPVolumeView alloc] initWithFrame:frame];
[[[[UIApplication sharedApplication] windows] objectAtIndex:0] insertSubview:self.volumeView atIndex:0];

self.launchVolume = [[MPMusicPlayerController applicationMusicPlayer] volume];
BOOL hadToLowerVolume = self.launchVolume == 1.0;
BOOL hadToRaiseVolume = self.launchVolume == 0.0;

// Avoid flashing the volume indicator
if (hadToLowerVolume || hadToRaiseVolume)
{
dispatch_async(dispatch_get_current_queue(), ^{
double delayInSeconds = 0.01;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
if( hadToLowerVolume )
{
[[MPMusicPlayerController applicationMusicPlayer] setVolume:0.95];
launchVolume = 0.95;
self.launchVolume = 0.95;
}

if( hadToRaiseVolume )
{
[[MPMusicPlayerController applicationMusicPlayer] setVolume:0.05];
launchVolume = 0.05;
self.launchVolume = 0.05;
}
});
});
}

CGRect frame = CGRectMake(0, -100, 10, 0);
self.volumeView = [[MPVolumeView alloc] initWithFrame:frame];
[self.volumeView sizeToFit];
[[[[UIApplication sharedApplication] windows] objectAtIndex:0] addSubview:self.volumeView];

[self initializeVolumeButtonStealer];

self.hadToLowerVolume = hadToLowerVolume;
self.hadToRaiseVolume = hadToRaiseVolume;


[self initializeVolumeButtonStealer];

if (!self.suspended)
{
// Observe notifications that trigger suspend
Expand Down Expand Up @@ -194,12 +203,12 @@ -(void)stopStealingVolumeButtonEvents

AudioSessionRemovePropertyListenerWithUserData(kAudioSessionProperty_CurrentHardwareOutputVolume, volumeListenerCallback, (__bridge void *)(self));

if( hadToLowerVolume )
if( self.hadToLowerVolume )
{
[[MPMusicPlayerController applicationMusicPlayer] setVolume:1.0];
}

if( hadToRaiseVolume )
if( self.hadToRaiseVolume )
{
[[MPMusicPlayerController applicationMusicPlayer] setVolume:0.0];
}
Expand Down
2 changes: 0 additions & 2 deletions VolumeSnap/VolumeSnap-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>
23 changes: 9 additions & 14 deletions VolumeSnap/en.lproj/MainStoryboard.storyboard
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="2.0" toolsVersion="3084" systemVersion="12E55" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" initialViewController="2">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="4514" systemVersion="13A3017" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" initialViewController="2">
<dependencies>
<deployment defaultVersion="1552" identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="2083"/>
<deployment defaultVersion="1280" identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3747"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="5">
<objects>
<viewController id="2" customClass="ViewController" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="3">
<rect key="frame" x="0.0" y="20" width="320" height="460"/>
<rect key="frame" x="0.0" y="0.0" width="320" height="480"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<view contentMode="scaleToFill" id="THB-7g-dvw">
<rect key="frame" x="0.0" y="0.0" width="320" height="324"/>
<rect key="frame" x="0.0" y="0.0" width="320" height="338"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES" flexibleMaxY="YES"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" text="&lt;--- Add 1" lineBreakMode="tailTruncation" minimumFontSize="10" id="Fwv-xt-nbb">
<rect key="frame" x="6" y="3" width="81" height="21"/>
<rect key="frame" x="6" y="17" width="81" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" text="&lt;--- Subtract 1" lineBreakMode="tailTruncation" minimumFontSize="10" id="bx5-bd-5aF">
<rect key="frame" x="6" y="51" width="138" height="21"/>
<rect key="frame" x="6" y="65" width="138" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
Expand All @@ -42,24 +42,19 @@
<nil key="highlightedColor"/>
</label>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="kFD-zD-obv">
<rect key="frame" x="20" y="359" width="280" height="37"/>
<rect key="frame" x="20" y="317" width="280" height="79"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="15"/>
<fontDescription key="fontDescription" type="system" pointSize="26"/>
<state key="normal" title="Start Stealing">
<color key="titleColor" red="0.19607843459999999" green="0.30980393290000002" blue="0.52156865600000002" alpha="1" colorSpace="calibratedRGB"/>
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<state key="highlighted">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="changeStealing:" destination="2" eventType="touchUpInside" id="pvo-L9-Ufa"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
</view>
<simulatedScreenMetrics key="simulatedDestinationMetrics"/>
<connections>
<outlet property="changeStealingButton" destination="kFD-zD-obv" id="dzp-kP-MiY"/>
<outlet property="counterLabel" destination="BfA-cG-mhI" id="3E3-aB-xwh"/>
Expand Down

0 comments on commit 8a7cd1c

Please sign in to comment.