使用AVPlayer获取AirPlay设备的名称

时间:2012-10-24 07:48:44

标签: ios avplayer

在MPMoviePlayerController中启用AirPlay时,它会显示文本“此视频正在设备名称上播放”。将AirPlay与AVPlayer一起使用时,有没有办法以编程方式获取设备名称?

4 个答案:

答案 0 :(得分:9)

从iOS7开始,currentRoute的AudioToolbox API已弃用:

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

(您要获得的是portType的{​​{1}} portDescription}:

audioSession.currentRoute

答案 1 :(得分:8)

在其他框架中搜索以获取您所连接的Apple TV的名称后,我终于在AudioToolbox框架中找到了此信息。可能有其他方法可以实现这一点,但到目前为止我还没有找到另一种方法。希望这会有所帮助。

您需要导入AudioToolbox框架:

#import <AudioToolbox/AudioToolbox.h>

然后在你想要检测airplay是否可用时调用的方法

- (BOOL)isAirplayActive {
  CFDictionaryRef currentRouteDescriptionDictionary = nil;
  UInt32 dataSize = sizeof(currentRouteDescriptionDictionary);
  AudioSessionGetProperty(kAudioSessionProperty_AudioRouteDescription, &dataSize, &currentRouteDescriptionDictionary);

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

答案 2 :(得分:6)

对于swift,我会发布一个类似于ambientlight的答案。也许它对未来的某些人有帮助。

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
}

Swift 3.0

private func addAirplayNotifier() {
    NotificationCenter.default.addObserver(self, selector: Selector("airplayChanged:"), name:NSNotification.Name.AVAudioSessionRouteChange, object: AVAudioSession.sharedInstance())
}

答案 3 :(得分:0)

快捷键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
    }
}