如何检查设备是否通过AirPlay连接?

4

我遇到了一个问题,需要检查我是否连接到AirPlay设备,并且它是通过镜像或流媒体连接的。但是这个检查需要在视频开始之前进行。

airPlayVideoActive仅在视频已经开始播放时返回YES。

4个回答

20

这是我的解决方案

- (BOOL)isAudioSessionUsingAirplayOutputRoute
{
    /**
     * I found no other way to check if there is a connection to an airplay device
     * airPlayVideoActive is NO as long as the video hasn't started 
     * and this method is true as soon as the device is connected to an airplay device
     */
    AVAudioSession* audioSession = [AVAudioSession sharedInstance];
    AVAudioSessionRouteDescription* currentRoute = audioSession.currentRoute;
    for (AVAudioSessionPortDescription* outputPort in currentRoute.outputs){
        if ([outputPort.portType isEqualToString:AVAudioSessionPortAirPlay])
            return YES;
    }
    return NO;
}

要检查AirPlay连接是否正在进行镜像,您只需要检查屏幕数即可。
if ([[UIScreen screens] count] < 2)) {
    //streaming
}
else {
    //mirroring
}

如果有更好的解决方案,请告诉我。

我需要在整个应用程序中检查它,所以我该在哪里编写这段代码呢?我很困惑,请帮帮我。 :) - Pravin Tate

4

Swift版本:

var isAudioSessionUsingAirplayOutputRoute: Bool {

    let audioSession = AVAudioSession.sharedInstance()
    let currentRoute = audioSession.currentRoute

    for outputPort in currentRoute.outputs {
        if outputPort.portType == AVAudioSessionPortAirPlay {
            return true
        }
    }

    return false
}

检查屏幕计数:

if UIScreen.screens.count < 2 {
    //streaming
} else {
    //mirroring
}

1

0

为那些在 Objc 上挣扎的可怜人

[[NSNotificationCenter defaultCenter]
    addObserver:self
    selector: @selector(deviceChanged:)
    name:AVAudioSessionRouteChangeNotification
    object:[AVAudioSession sharedInstance]];

- (void)deviceChanged:(NSNotification *)sender {
      NSLog(@"Enters here when connect or disconnect from Airplay");
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接