iOS的AirPlay:我的应用程序只有在镜像打开时通知外部显示器?

我正在尝试在我的应用中启用AirPlay支持。 我不是在做video 我想用外部显示器作为“第二显示器”。

这是我的问题:如果我从我的AirPlaybuttonselect“AppleTV”,我的应用程序不会收到通知。 我的应用程序唯一一次得到通知是当我离开我的应用程序时,进入操作系统级的AirPlaybutton,在那里select“AppleTV”并打开镜像。 如果我将镜像closures,我的应用程序会被告知外部显示器已经消失。

所以:

  1. 为什么当我从应用程序中select外部显示器时,我的应用程序不会收到通知?
  2. 为什么当我开启镜像时,我的应用程序会收到外部显示器的通知? 我显然是误解了一些东西,但是看起来好像转向镜像应该告诉我的应用程序外部显示已经消失(而不是现在可用,因为操作系统现在应该使用该外部显示器进行镜像)。

下面的代码示例。 提前感谢任何帮助!

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. // Is there already an external screen? if (UIScreen.screens.count > 1)] { [self prepareExternalScreen:UIScreen.screens.lastObject]; } // Tell us when an external screen is added or removed. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(externalScreenDidConnect:) name:UIScreenDidConnectNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(externalScreenDidDisconnect:) name:UIScreenDidDisconnectNotification object:nil]; self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; self.window.rootViewController = self.viewController; // Add AirPlay control to view controller. MPVolumeView* airplayButtonView = [[MPVolumeView alloc] init]; airplayButtonView.frame = CGRectMake(300, 300, 50, 50); airplayButtonView.backgroundColor = [UIColor blackColor]; airplayButtonView.showsVolumeSlider = NO; airplayButtonView.showsRouteButton = YES; [self.viewController.view addSubview:airplayButtonView]; [self.window makeKeyAndVisible]; return YES; } #pragma mark - External screen handling - (void)externalScreenDidConnect:(NSNotification*)notification { [self prepareExternalScreen:notification.object]; } - (void)externalScreenDidDisconnect:(NSNotification*)notification { // Don't need these anymore. self.externalWindow = nil; } - (void)prepareExternalScreen:(UIScreen*)externalScreen { NSLog(@"PREPPING EXTERNAL SCREEN."); self.viewController.view.backgroundColor = [UIColor blueColor]; CGRect frame = externalScreen.bounds; self.externalWindow = [[UIWindow alloc] initWithFrame:frame]; self.externalWindow.screen = externalScreen; self.externalWindow.hidden = NO; self.externalWindow.backgroundColor = [UIColor redColor]; } 

不幸的是,这是正确的。 辅助显示器(Airplay屏幕)仅适用于镜像。

这是一个应用程序,显示如何实现这个: https : //github.com/quellish/AirplayDemo

看看你的代码,当用户进入airplay菜单并在应用程序处于活动状态时打开镜像时,应该获得UIScreenDidConnectNotification。 MPVolumeView或电影控制器的“Airplaybutton”不控制镜像(以及外部显示function)。 不幸的是,video和audio输出与镜像分离,镜像只能使用全系统镜像UI来打开或closures。

底线:您无法在您的应用内打开AirPlay屏幕。

最后find答案,您必须启用镜像才能获得新的屏幕通知,但是您应该用第二个屏幕内容覆盖屏幕。 很混乱!

看到这个例子:

UIScreen屏幕总是返回1个屏幕

现在,最糟糕的部分。 您可以使用以下方式在应用程序内添加一个AirPlaybutton:

 MPVolumeView *volumeView = [ [MPVolumeView alloc] init] ; [view addSubview:volumeView]; 

但是你不能从这个select器启用镜像! 而且没有编程的方式来打开镜像。

如何以编程方式打开iPhone 4S上的AirPlay Screen Mirroring?

因此显然,获得第二个屏幕体验的唯一方法是指导用户如何从多任务栏打开AirPlay,并确保它们打开镜像。

从不幸的应用程序内部似乎不可能。 只有airplay声音可以从app afaik里面打开。 下面是一个使用OpenGL的第二个屏幕的示例应用程序,声音http://developer.apple.com/library/ios/samplecode/GLAirplay/Introduction/Intro.html