我遇到了一个问题,需要检查我是否连接到AirPlay设备,并且它是通过镜像或流媒体连接的。但是这个检查需要在视频开始之前进行。
airPlayVideoActive仅在视频已经开始播放时返回YES。
我遇到了一个问题,需要检查我是否连接到AirPlay设备,并且它是通过镜像或流媒体连接的。但是这个检查需要在视频开始之前进行。
airPlayVideoActive仅在视频已经开始播放时返回YES。
这是我的解决方案
- (BOOL)isAudioSessionUsingAirplayOutputRoute
{
/**
* I found no other way to check if there is a connection to an airplay device
* airPlayVideoActive is NO as long as the video hasn't started
* and this method is true as soon as the device is connected to an airplay device
*/
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
AVAudioSessionRouteDescription* currentRoute = audioSession.currentRoute;
for (AVAudioSessionPortDescription* outputPort in currentRoute.outputs){
if ([outputPort.portType isEqualToString:AVAudioSessionPortAirPlay])
return YES;
}
return NO;
}
if ([[UIScreen screens] count] < 2)) {
//streaming
}
else {
//mirroring
}
Swift版本:
var isAudioSessionUsingAirplayOutputRoute: Bool {
let audioSession = AVAudioSession.sharedInstance()
let currentRoute = audioSession.currentRoute
for outputPort in currentRoute.outputs {
if outputPort.portType == AVAudioSessionPortAirPlay {
return true
}
}
return false
}
检查屏幕计数:
if UIScreen.screens.count < 2 {
//streaming
} else {
//mirroring
}
为那些在 Objc 上挣扎的可怜人
[[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");
}