Vitul Goyal
Vitul Goyal

Reputation: 621

iOS detect Airplay during app launch

We are building an iOS app where users can watch videos under a subscription model.

We don't want users to Airplay the videos to any other device.

The video is being played inside a UIWebView.

I checked various online resources:

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

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

The above options have not worked.

Also, i tried this code but it always return 1.

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

I have tried this code as well:

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

This notification system works only when i launch the App and then turn on Airplay. If i turn on Airplay and then launch the App, then nothing is detected.

I need to detect if mirroring is on when the app launches. I have seen other apps do this so i am sure this is possible.

Please help.

Upvotes: 5

Views: 2297

Answers (1)

Ganesh Bavaskar
Ganesh Bavaskar

Reputation: 764

Try this solution.

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

Upvotes: 5

Related Questions