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?
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!
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;
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With