6

我正在尝试在我的应用中启用 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];
}
4

3 回答 3

8

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

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

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

底线:您无法从应用程序中打开该 AirPlay 屏幕。

于 2012-01-27T00:45:30.477 回答
2

终于找到了答案,您必须启用镜像才能获得新屏幕通知,但是您应该用您的第二个屏幕内容覆盖屏幕。非常混乱!

看这个例子:

UIScreen 屏幕总是返回 1 个屏幕

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

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

但是,您不能从此选择器启用镜像!并且没有编程方式来打开镜像。

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

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

于 2012-08-26T21:14:12.727 回答
0

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

于 2013-05-04T00:18:18.297 回答