Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get name of AirPlay device using AVPlayer

Tags:

ios

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?

like image 665
Anton Avatar asked Oct 24 '12 07:10

Anton


2 Answers

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
    }
}
like image 42
Tomas Avatar answered Oct 23 '22 05:10

Tomas


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())
}
like image 168
TomCobo Avatar answered Oct 23 '22 07:10

TomCobo