0

I'm working with AirPlay right now to display an AVPlayerLayer. Here is a snippet of my code:

        let secondScreen = UIScreen.screens()[1]
        secondScreen.overscanCompensation = UIScreenOverscanCompensation(rawValue: 3)!
        let screenBounds = secondScreen.bounds

        self.secondWindow = nil // free window when switching between two AirPlay devices
        self.secondWindow = UIWindow.init(frame: screenBounds)
        self.secondWindow?.screen = secondScreen

        layer.removeFromSuperlayer()
        layer.frame = screenBounds
        layer.videoGravity = AVLayerVideoGravityResizeAspect
        self.externalAirPlayView = nil // free view when switching between two AirPlay devices
        self.externalAirPlayView = UIView(frame: screenBounds)
        self.externalAirPlayView!.layer.addSublayer(layer)
        self.secondWindow?.addSubview(self.externalAirPlayView!)
        self.secondWindow?.makeKeyAndVisible()

This code usually works fine, but sometimes I get (0, 0, 0, 0) as the bounds of the external screen. I also get (0, 0, 0, 0) in the UIScreenDidConnectNotification. In both of these cases the AVPlayerLayer does not show up on the AirPlay device because the frame of the window is set incorrectly.

Just a note, if I get (0, 0, 0, 0) as the bounds even once, I will never get the correct bounds again until I either restart the app or I reinitialize the current view controller. Restarting the AirPlay device doesn't seem to help.

Is there a way to get the correct bounds of the external screen?

HarryL
  • 33
  • 4

1 Answers1

0

Okay I finally solved this one. You can find the correct screen size inside of the available modes property. Code looks something like this

for screenMode in secondScreen.availableModes {
    if screenMode.size.width != && screenMode.size.height != 0 {
        screenBounds.size = screenMode.size
        break
    }
}
HarryL
  • 33
  • 4