在 MPMoviePlayerController 中启用 AirPlay 时,它会显示文本“此视频正在设备名称上播放”。将 AirPlay 与 AVPlayer 一起使用时,有没有办法以编程方式获取设备名称?
问问题
5608 次
4 回答
10
从 iOS7 AudioToolbox API 开始, currentRoute 已弃用:
Apple 改为在 AudioSession 中为您提供 currentRoute API,它允许检索它的端口信息以及以一种很好的方式收听 audioRouteChangeNotification:
NSString* airplayName = [self activeAirplayOutputRouteName];
if (airplayName) {
//airplay is active
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteHasChangedNotification:) name:AVAudioSessionRouteChangeNotification object:[AVAudioSession sharedInstance]];
(你想要得到的是of portType
):portDescription
audioSession.currentRoute
- (NSString*)activeAirplayOutputRouteName
{
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
AVAudioSessionRouteDescription* currentRoute = audioSession.currentRoute;
for (AVAudioSessionPortDescription* outputPort in currentRoute.outputs){
if ([outputPort.portType isEqualToString:AVAudioSessionPortAirPlay])
return outputPort.portName;
}
return nil;
}
- (void)audioRouteHasChangedNotification:(NSNotification*)notification
{
//do something
}
于 2014-07-31T00:46:39.827 回答
8
在其他框架中搜索了你连接的Apple TV的名称后,我终于在AudioToolbox框架中找到了这个信息。可能还有其他方法可以做到这一点,但到目前为止我还没有找到其他方法。希望这可以帮助。
您需要导入 AudioToolbox 框架:
#import <AudioToolbox/AudioToolbox.h>
然后是当你想检测airplay是否可用时调用的方法
- (BOOL)isAirplayActive {
CFDictionaryRef currentRouteDescriptionDictionary = nil;
UInt32 dataSize = sizeof(currentRouteDescriptionDictionary);
AudioSessionGetProperty(kAudioSessionProperty_AudioRouteDescription, &dataSize, ¤tRouteDescriptionDictionary);
self.deviceOutputType = nil;
self.airplayDeviceName = nil;
if (currentRouteDescriptionDictionary) {
CFArrayRef outputs = CFDictionaryGetValue(currentRouteDescriptionDictionary, kAudioSession_AudioRouteKey_Outputs);
if(CFArrayGetCount(outputs) > 0) {
CFDictionaryRef currentOutput = CFArrayGetValueAtIndex(outputs, 0);
//Get the output type (will show airplay / hdmi etc
CFStringRef outputType = CFDictionaryGetValue(currentOutput, kAudioSession_AudioRouteKey_Type);
//If you're using Apple TV as your ouput - this will get the name of it (Apple TV Kitchen) etc
CFStringRef outputName = CFDictionaryGetValue(currentOutput, @"RouteDetailedDescription_Name");
self.deviceOutputType = (NSString *)outputType;
self.airplayDeviceName = (NSString *)outputName;
return (CFStringCompare(outputType, kAudioSessionOutputRoute_AirPlay, 0) == kCFCompareEqualTo);
}
}
return NO;
}
于 2013-01-17T21:18:25.987 回答
7
我将发布与环境光类似的答案,以便快速。也许它对将来的某人有帮助。
private func addAirplayNotifier() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("airplayChanged:"), name: AVAudioSessionRouteChangeNotification, object: AVAudioSession.sharedInstance())
}
func airplayChanged(sender:NSNotification) -> Bool {
var airplayConnected = false
let currentRoute = AVAudioSession.sharedInstance().currentRoute
for output in currentRoute.outputs {
if output.portType == AVAudioSessionPortAirPlay {
print("Airplay Device connected with name: \(output.portName)")
airplayConnected = true
}
}
print("Disconnect Airplay")
return airplayConnected
}
斯威夫特 3.0
private func addAirplayNotifier() {
NotificationCenter.default.addObserver(self, selector: Selector("airplayChanged:"), name:NSNotification.Name.AVAudioSessionRouteChange, object: AVAudioSession.sharedInstance())
}
于 2016-02-18T10:47:41.467 回答
3
斯威夫特 4
private func addAirplayNotifier() {
NotificationCenter.default.addObserver(
self,
selector: #selector(airplayChanged),
name: AVAudioSession.routeChangeNotification,
object: AVAudioSession.sharedInstance())
}
@objc func airplayChanged() {
isAirPlaying = false
let currentRoute = AVAudioSession.sharedInstance().currentRoute
for output in currentRoute.outputs where output.portType == AVAudioSession.Port.airPlay {
print("Airplay Device connected with name: \(output.portName)")
isAirPlaying = true
}
}
于 2018-11-29T12:39:55.303 回答