Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the device is connected via airplay?

I had the problem to check if I am connected to an airplay device and if it is connected via mirroring or streaming. But the check needs to be done before the video started.

airPlayVideoActive only return YES if the video already started.

like image 487
sebastian friedrich Avatar asked Nov 29 '22 14:11

sebastian friedrich


2 Answers

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
}
like image 27
Sean Inge Asbjørnsen Avatar answered Dec 06 '22 23:12

Sean Inge Asbjørnsen


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

like image 71
sebastian friedrich Avatar answered Dec 07 '22 00:12

sebastian friedrich