title: AirPlay
date: 2016-07-16
tags: AirPlay,AirPlay設備自動選擇
博客地址
AirPlay
需求:繞過系統(tǒng)限制,自動選擇支持
AirPlay的設備
Airplay基礎知識
要調(diào)用出AirPlay列表需要使用到MPVolumeView控件,當系統(tǒng)檢測到網(wǎng)絡環(huán)境中有支持AirPlay的設備時才會出現(xiàn)AirPlay圖標,用戶點擊這個圖標,呼出支持AirPlay的設備列表。
MPVolumeView *volumView = [[MPVolumeView alloc] initWithFrame:CGRectMake(60, 100, 40, 40)];
[volumView setRouteButtonImage:[UIImage imageNamed:@"pic-02"] forState:UIControlStateNormal];
volumView.showsVolumeSlider = NO;
setRouteButtonImage自定義AirPlay圖標
自動選擇AirPlay設備
自動選擇AirPlay設備要使用了MediaPlayer.framework的私用類MPAVRoutingController,在這里可以搜到iOS所有的framework的頭文件。
Class MPAVRoutingController = NSClassFromString(@"MPAVRoutingController");
self.routerController = [[MPAVRoutingController alloc] init];
[self.routerController setValue:self forKey:@"delegate"];
[self.routerController setValue:[NSNumber numberWithLong:2] forKey:@"discoveryMode"];
NSClassFromString獲取MPAVRoutingController類定義,并通過KVC給它的屬性賦值,discoveryMode為2時才能及時在AirPlay設備發(fā)生變化時觸發(fā)它協(xié)議的-(void)routingControllerAvailableRoutesDidChange:(id)arg1;方法
-(void)routingControllerAvailableRoutesDidChange:(id)arg1{
if (self.deviceName == nil) {
return;
}
NSArray *availableRoutes = [self.routerController valueForKey:@"availableRoutes"];
for (id router in availableRoutes) {
NSString *routerName = [router valueForKey:@"routeName"];
if ([routerName rangeOfString:self.deviceName].length >0) {
BOOL picked = [[router valueForKey:@"picked"] boolValue];
if (picked == NO) {
[self.routerController performSelector:@selector(pickRoute:) withObject:router];
}
return;
}
}
}
在
MPAVRoutingControllerDelegate的routingControllerAvailableRoutesDidChange用代碼來選擇用戶指定的AirPlay設備,如果用戶想取消自動選擇的功能,將deviceName屬性賦值為nil即可。