-
Notifications
You must be signed in to change notification settings - Fork 7
/
AirPlayDetector.m
71 lines (59 loc) · 2.21 KB
/
AirPlayDetector.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// AirPlayDetector.m
//
// Created by Steve Potter on 5/11/12.
//
#import "AirPlayDetector.h"
#import <Foundation/Foundation.h>
#import <MediaPlayer/MPMoviePlayerController.h>
#import <MediaPlayer/MediaPlayer.h>
NSString *AirPlayAvailabilityChanged = @"AirPlayAvailabilityChanged";
@implementation AirPlayDetector
+ (AirPlayDetector *)defaultDetector
{
static AirPlayDetector *defaultDetector;
@synchronized(self)
{
if (!defaultDetector)
defaultDetector = [[AirPlayDetector alloc] init];
return defaultDetector;
}
}
- (BOOL)isAirPlayAvailable
{
return isAirPlayAvailable;
}
- (void)startMonitoring:(UIWindow*)window;
{
//here is the real trick. place an MPVolumeView in the window and monitor for changes in the airplay button's alpha property. note that this depends on the MPVolumeView's view hierarchy so it must be tested for each iOS release
//this was made possible by the awesome sample from http://stackoverflow.com/questions/5388884/airplay-button-on-custom-view-problems
isAirPlayAvailable = FALSE;
volumeButton = [[MPVolumeView alloc] initWithFrame:CGRectMake(-1000,-1000,100,100)];
volumeButton.showsVolumeSlider = NO;
volumeButton.showsRouteButton = YES;
[window addSubview:volumeButton];//if you don't add to a window, nothing will ever happen
for (UIView *view in volumeButton.subviews) {
if ([view isKindOfClass:[UIButton class]]) {
airplayButton = [view retain];
[airplayButton addObserver:self forKeyPath:@"alpha" options:NSKeyValueObservingOptionNew context:nil];
}
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (![object isKindOfClass:[UIButton class]])
return;
BOOL isNowAvailable = [[change valueForKey:NSKeyValueChangeNewKey] floatValue] == 1;
if ( isNowAvailable != isAirPlayAvailable )
{
isAirPlayAvailable = isNowAvailable;
[[NSNotificationCenter defaultCenter] postNotificationName:AirPlayAvailabilityChanged object:self];
}
}
- (void)dealloc
{
[airplayButton removeObserver:self forKeyPath:@"alpha"];
[airplayButton release];
[volumeButton release];
[super dealloc];
}
@end