sankar siva
sankar siva

Reputation: 255

Is there any notification for detecting AirPlay in Objective-C?

I am using MPVolumeView for showing Airplay icon and it works fine.

But I need to show an animation when Airplay network comes, and hide that animation when airplay network hides.

Is there a notification that will let me know when Airplay starts and ends?

Upvotes: 21

Views: 11073

Answers (6)

MrRobot
MrRobot

Reputation: 309

If you want a notification here is the way to do it

[[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");
}

Upvotes: 1

Martin
Martin

Reputation: 12215

6 years later. I think Sankar Siva did not ask for detecting, but for activating an airplay route.

I've upped @Alf because he placed me on the right direction, but he is not answering to the question.

MPVolumeViewWirelessRoutesAvailableDidChangeNotification fires when MPVolumeView detects a new route.

On the other hand, MPVolumeViewWirelessRouteActiveDidChangeNotification fires when a new route is taken, eg: when you select your Apple TV for example.

No need of private API.

Upvotes: 0

Micky
Micky

Reputation: 5728

There's a MPVolumeViewWirelessRoutesAvailableDidChangeNotification since iOS 7 you can register for.

Upvotes: 7

ambientlight
ambientlight

Reputation: 7332

To be precise: To check exactly for airplay with public API: NO

All you can do with public API is to check for available wireless routes, which includes airplay in it: (In simple case when you have a MPVolumeView instance hooked up somewhere to your view, you can just call volumeView.areWirelessRoutesAvailable;)

If you are curious how to check if exactly airplay is available with private API:

- (BOOL)isAirplayAvailable
{
    Class MPAVRoutingController = NSClassFromString(@"MPAVRoutingController");
    id routingController = [[MPAVRoutingController alloc] init];

    NSArray* availableRoutes = [routingController performSelector:@selector(availableRoutes)];
    for (id route in availableRoutes) {
        NSDictionary* routeDescription = [route performSelector:@selector(avRouteDescription)];
        if ([routeDescription[@"AVAudioRouteName"] isEqualToString:@"AirTunes"])
            return true;
    }

    return false;
}

(And in fact MPVolumeView has an MPAVRoutingController instance as its ivar, so the -areWirelessRoutesAvailable is just an accessor exactly for [volumeView->_routingController wirelessDisplayRoutesAvailable])

Also AVAudioSession exposes currentRoute to you, so you do can check if airplay is active easily with:

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

    return false;
}

(the answer about AirPlayDetector doesn't guarantee that Airplay is available - all it does it checks the alpha value of MPVolumeView's routeSelection button, which will be shown in any case when wireless routes are available, bluetooth for example. It will do exactly the same as volumeView.areWirelessRoutesAvailable;)

Upvotes: 8

Roman B.
Roman B.

Reputation: 3638

It can be done much easier with ReactiveCocoa. Check it out:

MPVolumeView *myVolumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(0, 0, 180, 22)];
for (UIView *view in myVolumeView.subviews) {
    if ([view isKindOfClass:[UIButton class]]) {
        [[RACAbleWithStart(view, alpha) distinctUntilChanged] subscribeNext:^(id x) {
            NSLog(@"airplay button visibility changed %@", x);
        }];
        [[RACAbleWithStart(view, frame) distinctUntilChanged] subscribeNext:^(id x) {
            NSLog(@"airplay button connection changed %@", x);
        }];
    }
}

Upvotes: 3

Steve Potter
Steve Potter

Reputation: 1919

This is exactly what you're looking for - https://github.com/StevePotter/AirPlayDetector

It is a single class that provides a property to determine whether airplay devices are active. And a notification when availability changes.

Using it is simple. Like, to determine availability you write:

[AirPlayDetector defaultDetector].isAirPlayAvailable

Enjoy!

Upvotes: 18

Related Questions