Objective-C中有没有检测AirPlay的通知?

我正在使用MPVolumeView显示Airplay图标,它工作正常。

但是,当Airplaynetworking来临时,我需要显示一个animation,并在播放networking隐藏时隐藏该animation。

是否有通知让我知道Airplay何时开始和结束?

这正是你正在寻找 – https://github.com/StevePotter/AirPlayDetector

它是一个单独的课程,提供一个属性来确定airplay设备是否处于活动状态。 并在可用性变化时发出通知。

使用它很简单。 就像,要确定你写的可用性:

[AirPlayDetector defaultDetector].isAirPlayAvailable 

请享用!

准确地说:要使用公共API 准确检查airplay: NO

您可以使用公共API来做的是检查可用的无线路由 ,其中包括airplay:(在简单情况下,当您将MPVolumeView实例连接到视图的某处时,可以调用volumeView.areWirelessRoutesAvailable;

如果您好奇如何检查是否可以使用私有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; } 

(实际上MPVolumeView有一个MPAVRoutingController实例作为它的ivar,所以-areWirelessRoutesAvailable只是一个访问器,正好适用于[volumeView->_routingController wirelessDisplayRoutesAvailable]

此外AVAudioSession暴露currentRoute给你,所以你可以检查airplay是否活动很容易:

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

(关于AirPlayDetector的回答并不能保证Airplay是可用的 – 所有这一切都会检查MPVolumeView的routeSelectionbutton的alpha值,当无线路由可用时(例如蓝牙),将显示任何情况。作为volumeView.areWirelessRoutesAvailable;

从iOS 7开始,您可以注册一个MPVolumeViewWirelessRoutesAvailableDidChangeNotification。

使用ReactiveCocoa可以轻松完成。 一探究竟:

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