使用AVPlayer获取AirPlay设备的名称 [英] Get name of AirPlay device using AVPlayer

查看:577
本文介绍了使用AVPlayer获取AirPlay设备的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When AirPlay is enabled in a MPMoviePlayerController, it displays a text "This video is playing on device name ". When using AirPlay with an AVPlayer, is there any way to programatically get the device name?

推荐答案

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

After searching in other frameworks to get the name of the Apple TV you're connected to, I finally found this information in the AudioToolbox framework. There may be other ways to get this, but so far I have not found another way. Hope this helps.

您需要导入AudioToolbox框架:

You'll need to import the AudioToolbox framework:

#import <AudioToolbox/AudioToolbox.h>

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

and then a method to call when you want to detect if airplay is available

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

这篇关于使用AVPlayer获取AirPlay设备的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆