Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Airplay - "Playing on your TV"

I have displayed the airplay button using this code

var airplayButton: UIBarButtonItem!
let airView: MPVolumeView = MPVolumeView()
airView.showsRouteButton = true
airView.showsVolumeSlider = false
airView.sizeToFit()
airView.tintColor = UIColor.blackColor()

airplayButton = UIBarButtonItem(customView: airView)
airplayButton.tintColor = UIColor.whiteColor()

Now, I want to display a screen. Is there any default method in iOS Airplay framework to display it. Or I have to design screen myself. Also, there are no delegates to verify when the device is connected and movie start streaming on AppleTV through iOS Device. I only have a variable to check i.e externalPlaybackActive

The issue is If I use the variable it wouldn't be efficient solution as I have airplay can be connected from Control during playback. I don't want to run a timer to check after each second if the movie is streaming on AppleTV. Any better ideas ?

This video is playing on "Apple TV"

Like this image

like image 223
Taimur Ajmal Avatar asked Jun 30 '16 09:06

Taimur Ajmal


People also ask

How can I play AirPlay on my TV?

Stream content from your Apple device to the TV: If AirPlay has not been configured, press the (Input select) button on the TV remote control and select (AirPlay), then select AirPlay & HomeKit settings and turn on AirPlay. Make sure your Apple device is connected to the same network as the TV.

Can you AirPlay an iPhone to a TV?

Stream video from your iPhone, iPad, or iPod touch to a TV Tap AirPlay . In some apps, you might need to tap a different button first. For example, in the Photos app, tap Share , then tap AirPlay . Choose your Apple TV or AirPlay 2-compatible smart TV.

Why can't I play AirPlay on my TV?

For AirPlay to work, your devices need to be near each other and turned on. If you are trying to AirPlay to your Apple TV, make sure it is awake and not in Sleep mode. It is impossible to enjoy AirPlay and Apple TV mirroring without the internet unless you use peer-to-peer AirPlay.

Why won't my iPhone let me AirPlay to my TV?

Make sure that your AirPlay-compatible devices are turned on and near each other. Check that the devices are updated to the latest software and are on the same Wi-Fi network. Restart the devices that you want to use with AirPlay or screen mirroring.


1 Answers

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

}
like image 73
Taimur Ajmal Avatar answered Sep 28 '22 12:09

Taimur Ajmal