如何在隔空播放中区分苹果电视和其他设备



我正在尝试通过隔空播放连接苹果电视,但问题是如果我连接任何其他外部设备(如蓝牙)或其他一些设备,它显示为在窗口中连接的设备。所以我想确定连接了哪个设备,我只有在连接苹果电视时才必须启用。

如何识别它是苹果电视还是其他设备?

这就是我创建隔空播放自定义按钮的方式

  for (UIButton *button in volumeView.subviews) {
            if ([button isKindOfClass:[UIButton class]]) {
                self.airplayButton = (UIButton*)button;
                button.frame = CGRectMake(0, 0, 30, 23);
                button.backgroundColor = [UIColor clearColor];
                [self.airplayButton addObserver:self forKeyPath:@"alpha" options:NSKeyValueObservingOptionNew context:nil];
            }
        }

因此,即使连接了其他一些设备,alpha 也会为按钮而变化。

我之前研究过这个问题,没有简单的方法来确定连接的设备是否是 Apple TV,有一个 Airplay 选择器可以做到这一点,但它背后的代码/功能似乎不可用。

最好的办法是监视添加/删除的其他屏幕,然后仅在屏幕具有执行所需操作的功能时才显示外部内容。

我之前在某处读到,您可以获得Airplay设备的功能并使用此信息来检测Apple TV,但不幸的是,我目前找不到它。如果我找到它,我会添加评论。

目前,您最好的选择是使用本指南中描述的概念

提供的代码是 objective-c 格式的,但它很容易转换为 swift,这是您应该查看的主要部分

- (void)checkForExistingScreenAndInitializeIfPresent
{
    if ([[UIScreen screens] count] > 1)
    {
        // Get the screen object that represents the external display.
        UIScreen *secondScreen = [[UIScreen screens] objectAtIndex:1];
        // Get the screen's bounds so that you can create a window of the correct size.
        CGRect screenBounds = secondScreen.bounds;

        self.secondWindow = [[UIWindow alloc] initWithFrame:screenBounds];
        self.secondWindow.screen = secondScreen;

        // Set up initial content to display...
        // Show the window.
        self.secondWindow.hidden = NO;
    }
}

就像我说的,你可以对此进行编码,以便它检查设备是否支持某些分辨率,以便您可以排除不支持你的UI的设备

一些其他资源:https://developer.apple.com/airplay/

最新更新