This is how I implemented it. It works like a charm !
//Airplay constants
private var observerContextAirplay = 1
private var propertyToObserveAirplay = "externalPlaybackActive"
// MARK: AirPlay Key-value Observing
func startObservingForAirPlayStatusChanges()
{
self.player.moviePlayer.addObserver(self, forKeyPath: propertyToObserveAirplay, options: .New, context: &observerContextAirplay)
}
func stopObservingForAirPlayStatusChanges()
{
self.player.moviePlayer.removeObserver(self, forKeyPath: propertyToObserveAirplay)
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if context == &observerContextAirplay {
if self.player.moviePlayer.externalPlaybackActive == true
{
self.setUpAirPlayView()
}
else if self.player.moviePlayer.externalPlaybackActive == false
{
self.resetPlayerView()
}
}
else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
}
//Setup AirplayView
func setUpAirPlayView()
{
//Airplay Button
if self.airPlay_PlayBtn == nil
{
let playImage = UIImage(named: "player_play.png")!
if let _ = self.airPlay_PlayBtnFrame
{
self.airPlay_PlayBtn = UIButton(frame: self.airPlay_PlayBtnFrame)
self.airPlay_PlayBtn.setBackgroundImage(playImage, forState: UIControlState.Normal)
self.airPlay_PlayBtn.addTarget(self, action: #selector(ICFPlayerViewController.airPlayButtonAction), forControlEvents: UIControlEvents.TouchUpInside)
self.airPlay_PlayBtn.setBackgroundImage(UIImage(named: "player_play.png"), forState: .Normal)
self.airPlay_PlayBtn.setBackgroundImage(UIImage(named: "player_pause.png"), forState: .Selected)
self.airPlay_PlayBtn.center = self.view.center
self.view.addSubview(self.airPlay_PlayBtn)
if let _ = self.player
{
self.player.playPauseButton.hidden = true
}
}
}
else
{
self.airPlay_PlayBtn.hidden = false
if let _ = self.player
{
self.player.playPauseButton.hidden = true
}
}
// Airplay Label
if self.airPlayLabel == nil
{
self.airPlayLabel = UILabel()
self.airPlayLabel.frame = CGRectMake(0, 0, 280, 20)
self.airPlayLabel.text = "Your video is now playing on Apple TV"
self.airPlayLabel.textAlignment = NSTextAlignment.Center
self.airPlayLabel.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.6)
self.airPlayLabel.textColor = UIColor.whiteColor()
self.airPlayLabel.sizeToFit()
self.airPlayLabel.center = self.view.center
self.airPlayLabel.center.y = self.view.center.y - self.activityIndicator.frame.size.height*1.5
self.view.addSubview(self.airPlayLabel)
}
else
{
self.airPlayLabel.hidden = false
}
// Thumbnail
self.setupContentThumbnailImageView() //Fetch Thumbnail image
if let _ = self.thumbnailImage
{
self.view.addSubview(self.thumbnailImage)
}
self.view.bringSubviewToFront(bottomToolbar)
self.view.bringSubviewToFront(topToolbar)
if let _ = self.airPlayLabel
{
self.view.bringSubviewToFront(self.airPlayLabel)
}
if let _ = self.airPlay_PlayBtn
{
self.view.bringSubviewToFront(self.airPlay_PlayBtn)
}
}
self.setUpAirPlayView()
does. Did you take my suggestion and use thecontentOverlayView
? – Malevolent