我正在使用MPVolumeView来显示Airplay图标,并且它运行正常。
但是当Airplay网络出现时,我需要展示一个动画,并在Airplay网络隐藏时隐藏该动画。
是否存在通知可让我知道何时开始和结束Airplay?
我正在使用MPVolumeView来显示Airplay图标,并且它运行正常。
但是当Airplay网络出现时,我需要展示一个动画,并在Airplay网络隐藏时隐藏该动画。
是否存在通知可让我知道何时开始和结束Airplay?
这正是您所需要的 - https://github.com/StevePotter/AirPlayDetector
它是一个单一的类,提供了一个属性来确定Airplay设备是否处于活动状态,并在可用性发生变化时通知。
使用它很简单。例如,要确定可用性,您可以编写:
[AirPlayDetector defaultDetector].isAirPlayAvailable
享受吧!
确切地说: 使用公共API精确检查Airplay: 不行
你能用公共API做的只是检查可用的无线路由,其中包括了Airplay: (在一个简单的情况下,当你在某处连接了MPVolumeView
实例到你的视图上时,你可以直接调用volumeView.areWirelessRoutesAvailable;
)
如果你好奇如何使用私有API来检查是否确切可用Airplay:
- (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
实例作为其成员变量, 所以-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的routeSelection按钮的alpha值,当无线路由可用时(例如蓝牙),该按钮将在任何情况下显示。它将与volumeView.areWirelessRoutesAvailable;
完全相同。
isExternalPlaybackActive
与AVPlayerItem一起加载时,对我来说非常完美。谢谢! - iGranDav使用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);
}];
}
}
如果你想要通知,这里是实现方式
[[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");
}
六年后。 我认为 Sankar Siva 并没有要求检测,而是要求激活 airplay 路线。
我提到了 @Alf,因为他引导了我的方向,但他并没有回答问题。
MPVolumeViewWirelessRoutesAvailableDidChangeNotification
在 MPVolumeView 检测到新路线时触发。
另一方面,MPVolumeViewWirelessRouteActiveDidChangeNotification
在选取新路线时触发,例如:当你选择你的 Apple TV 时。
不需要私有 API。