我正在尝试在我的应用中启用AirPlay支持.我不是在做视频;我想将外部显示器用作“第二显示器”.
这是我的问题:如果我从AirPlay按钮选择“AppleTV”,我的应用程序不会收到通知.我的应用程序获得通知的唯一一次是当我离开我的应用程序时,转到操作系统级别的AirPlay按钮,在那里选择“AppleTV”并打开镜像.如果我关闭镜像,我的应用程序会被告知外部显示器已经消失.
所以:
>当我从中选择外部显示器时,为什么我的应用程序不会收到通知
在我的应用程序?
>为什么我的应用程序会收到通知
我开镜像时的外部显示……而不是之前?我显然误解了一些东西,但是看起来像镜像应该通知我的应用程序外部显示器已经消失(而不是现在可用,因为操作系统现在应该使用外部显示器进行镜像.)
代码示例如下.在此先感谢您的帮助!
- (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
查看代码时,当用户进入播放菜单并在应用程序处于活动状态时打开镜像时,您应该获取UIScreenDidConnectNotification. MPVolumeView或电影控制器的“Airplay按钮”不控制镜像(因此也不控制外部显示功能).不幸的是,视频和音频输出与镜像是分开的,只能使用系统范围的镜像UI打开或关闭镜像.
结论:您无法在应用内打开AirPlay屏幕.