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?
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
}
}
I'll post a similar answer than ambientlight for swift. Maybe it is helpful 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())
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With