iOS AirPlay:只有当镜像打开时,我的应用程序才会收到外部显示的通知



我正在尝试在我的应用程序中启用AirPlay支持。我不是在拍视频;我想将外部显示器用作"第二个显示器"。

我的问题是:如果我从AirPlay按钮中选择"AppleTV",我的应用程序不会收到通知。我的应用程序唯一得到通知的时间是当我离开应用程序时,转到操作系统级别的AirPlay按钮,在那里选择"AppleTV"并打开镜像。如果我关闭镜像,我的应用程序就会被告知外部显示器不见了。

因此:

  1. 为什么当我从中选择外部显示器时,我的应用程序没有收到通知在我的应用程序中
  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];
}

很遗憾,这是正确的。辅助显示屏(播放屏幕)仅在镜像时可用。

以下是一个应用程序,展示了如何实现此功能:https://github.com/quellish/AirplayDemo

查看您的代码,当用户转到airplay菜单并在您的应用程序处于活动状态时打开镜像时,您应该会收到UIScreenDirConnectionNotification。"播放按钮",无论是MPVolumeView还是电影控制器,都不控制镜像(因此也不控制外部显示功能)。不幸的是,视频和音频输出与镜像分离,只能使用系统范围的镜像UI打开或关闭镜像。

一句话:你不能在你的应用程序中打开AirPlay屏幕。

最终找到了答案,您必须启用镜像才能获得新屏幕通知,但随后您应该用第二个屏幕内容覆盖屏幕。非常令人困惑!

参见此示例:

UI屏幕总是返回1个屏幕

现在,最糟糕的部分。您可以使用以下内容在应用程序中添加AirPlay按钮:

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

但是,您无法从此选取器启用镜像!而且没有任何编程方式可以打开镜像。

如何以编程方式在iPhone 4S上打开AirPlay屏幕镜像

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

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

最新更新