Get AirPlay device name using AVPlayer

When AirPlay is included in the MPMoviePlayerController, it displays the text "This video is playing in the device name." When using AirPlay with AVPlayer, is there a way to programmatically get the device name?

+10
source share
4 answers

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

You will need to import the AudioToolbox framework:

#import <AudioToolbox/AudioToolbox.h> 

and then the call method when you want to determine if the broadcast 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; } 
+8
source

Starting with iOS7, the AudioToolbox API for currentRoute is becoming obsolete:

Instead, Apple provided you with access to the current API in AudioSession, which allows you to obtain port information and also listen to audio recording using the "AudioRouteChangeNotification" function:

 NSString* airplayName = [self activeAirplayOutputRouteName]; if (airplayName) { //airplay is active } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteHasChangedNotification:) name:AVAudioSessionRouteChangeNotification object:[AVAudioSession sharedInstance]]; 

(what you want to get is 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 } 
+9
source

I will send a similar answer than ambientlight for quick. Perhaps this is useful for someone in the future.

 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()) } 
+6
source

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 } } 
+2
source

All Articles