AirPlay
在iPhone端無法顯示Mirroring選項,連接后只能傳輸音頻信號.
@property (nonatomic)UIViewController *routerController;
@property (nonatomic,strong)NSString *deviceName;
@property (strong, nonatomic) UIPopoverController *airplayPopoverController;
- (void)showAirPlay
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
MPVolumeView *volumView = [[MPVolumeView alloc] initWithFrame:CGRectMake(60, 100, 40, 40)];
[volumView setRouteButtonImage:[UIImage imageNamed:@"class1_start"] forState:UIControlStateNormal];
volumView.showsVolumeSlider = NO;
weakSelf.deviceName = nil;
[weakSelf.view addSubview:volumView];
Class MPAVRoutingController = NSClassFromString(@"MPAVRoutingController");
weakSelf.routerController = [[MPAVRoutingController alloc] init];
[weakSelf.routerController setValue:weakSelf forKey:@"delegate"];
[weakSelf.routerController setValue:[NSNumber numberWithLong:2] forKey:@"discoveryMode"];
}
else
{
if(self.airplayPopoverController == nil)
{
// create Class
id TheClass = NSClassFromString(@"MPAudioVideoRoutingPopoverController");
// create Obj
id testObject = [[TheClass alloc] initWithContentViewController: weakSelf];
//通過方法名字符串create SEL
NSString *selectorStr = @"initWithType:includeMirroring:";
SEL initMethod = NSSelectorFromString(selectorStr);
if([testObject respondsToSelector: initMethod])
{
NSMethodSignature* signature = [TheClass instanceMethodSignatureForSelector: initMethod];
//使用方法的簽名來創(chuàng)建一個NSInvocation對象
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature: signature];
//要執(zhí)行誰的(target)的哪個方法(selector)
[invocation setTarget: testObject];
[invocation setSelector:initMethod];
NSInteger type = 0;
BOOL showMirroring = YES;
static id returnObject = nil;
//使用setArgument:atIndex:方法給要執(zhí)行的方法設置參數(shù)焰望,注意下標從2開始,因為0基公、1已經(jīng)被target與selector占用
[invocation setArgument:&type atIndex:2];
[invocation setArgument:&showMirroring atIndex:3];
[invocation retainArguments];
//執(zhí)行方法
[invocation invoke];
//returnObject獲取返回值
[invocation getReturnValue: &returnObject];
weakSelf.airplayPopoverController = returnObject;
}
}
UIButton *button = sender;
[weakSelf.airplayPopoverController presentPopoverFromRect:button.frame
inView:weakSelf.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
}
-(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)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
[self.routerController performSelector:@selector(pickRoute:) withObject:router];
#pragma clang diagnostic pop
}
return;
}
}
}
說明:
私有API,使用需謹慎~demo上玩一下就好.