Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 743
sankar siva Avatar asked Feb 14 '12 05:02

sankar siva


2 Answers

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!

like image 109
Steve Potter Avatar answered Dec 25 '22 18:12

Steve Potter


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;)

like image 34
ambientlight Avatar answered Dec 25 '22 17:12

ambientlight