Подтвердить что ты не робот

Получить имя устройства AirPlay с помощью AVPlayer

Когда AirPlay включен в MPMoviePlayerController, он отображает текст "Это видео воспроизводится на имя устройства". При использовании AirPlay с AVPlayer существует ли способ программно получить имя устройства?

4b9b3361

Ответ 1

После поиска в других фреймах, чтобы получить имя Apple TV, к которому вы подключены, я, наконец, нашел эту информацию в структуре AudioToolbox. Могут быть другие способы получить это, но до сих пор я не нашел другого пути. Надеюсь, это поможет.

Вам нужно будет импортировать фреймворк AudioToolbox:

#import <AudioToolbox/AudioToolbox.h>

а затем метод вызова, когда вы хотите определить, доступна ли трансляция

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

Начиная с iOS7 API AudioToolbox для currentRoute становится устаревшим:

Apple вместо этого предоставила вам доступ к текущему API-интерфейсу в AudioSession, что позволяет получить информацию о порте, а также прослушивать аудиозапись с помощью функции "AudioRouteChangeNotification":

NSString* airplayName = [self activeAirplayOutputRouteName];
if (airplayName) {
    //airplay is active

}

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteHasChangedNotification:) name:AVAudioSessionRouteChangeNotification object:[AVAudioSession sharedInstance]];

(то, что вы хотите получить, это portType of portDescription of 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
}

Ответ 3

Я отправлю аналогичный ответ, чем 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())
}

Ответ 4

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