iOS 在应用启动期间检测 Airplay

iOS detect Airplay during app launch

我们正在构建一个 iOS 应用程序,用户可以在其中以订阅模式观看视频。

我们不希望用户将视频播放到任何其他设备。

正在 UIWebView 中播放视频。

我查看了各种在线资源:

  1. https://developer.apple.com/documentation/uikit/uiwebview/1617973-mediaplaybackallowsairplay?language=objc

  2. https://github.com/MobileVet/AirPlayDetector

以上选项均无效。

此外,我试过这段代码,但它总是 return 1.

if ([[UIScreen screens] count] < 2)) {
//streaming
}
else {
//mirroring
}

我也试过这段代码:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveAirPlayNotification:) name: UIScreenDidConnectNotification object:nil];

此通知系统仅在我启动应用程序然后打开 Airplay 时才有效。如果我打开 Airplay 然后启动应用程序,则没有检测到任何内容。

我需要在应用启动时检测镜像是否开启。我已经看到其他应用程序这样做,所以我相信这是可能的。

请帮忙。

试试这个解决方案。

- (BOOL)isAirplayOn
{
    AVAudioSession* audioSession = [AVAudioSession sharedInstance];
    AVAudioSessionRouteDescription* currentRoute = audioSession.currentRoute;
    for (AVAudioSessionPortDescription* outputPort in currentRoute.outputs){
        if ([outputPort.portType isEqualToString:AVAudioSessionPortAirPlay])
            return YES;
    }
    return NO;
}