How to check if the device is connected via airplay?

南楼画角 提交于 2019-12-02 20:59:45

This is my solution

- (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;
}

To check if the airplay connection is mirroring you just have to check the screens count.

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

If there is a better solution, let me know

Swift version:

var isAudioSessionUsingAirplayOutputRoute: Bool {

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

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

    return false
}

And checking the screen count:

if UIScreen.screens.count < 2 {
    //streaming
} else {
    //mirroring
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!