AVPlayerViewController as a child VC - focus help

Hi,


I'm really enjoying programming w/ tvOS so far but I'm having trouble giving correct focus to an AVPlayerViewController that I set up as a child view controller via a container view in IB.


Right now the focus is sort of working. I can move it onto the AVPlayerVC...I can tell it's somewhere there because the Play/Pause button works with the video. However, the controls never appear and I can't seem to figure out which view is actually in focus so I can adjust my interface accordingly. When I check the mainScreen for it's focusedView, I get this response:


<_AVFocusContainerView 0x7fd57b4803a0; frame = {{0, 0}, {1220, 905}}; focused = YES; not full screen (playback is non-interactive); parent focus environment is NOT AVPlayerViewController: <AVNowPlayingPlaybackControlsViewController: 0x7fd57c049400>>

I have tried subclassing AVPlayerViewController to override the preferredFocusView using the below code, but that doesn't seem to have any effect.


    override var preferredFocusedView: UIView? {
        return view
    }


Any help would be greatly appreciated!

Hi bjhstudios,


First, please note the following line from our documentation:

Do not subclass AVPlayerViewController. Overriding this class’s methods is unsupported and results in undefined behavior.


It would be best not to subclass, as issues may arise later on.


AVPlayerViewController is designed such that when in full screen, the full playback experience (scrubbing, info panel access, etc) are all available. When in a space less than full screen, the assumption is that it is just one of several interactive elements on screen, and thus the view should not absorb all touch surface events as transport control.


If you wish to show that the view is in focus, you can perform a transform on it or provide some other visual indication by overriding the following method in your parent view controller and using the animation coordinator to manage those indicative adjustments:

/// Called when the screen’s focusedView has been updated to a new view. Use the animation coordinator to schedule focus-related animations in response to the update.
public func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator)


I hope that helps. If there are additional scenarios that you would like AVPlayerViewController to handle, please file an enhancement request at bugreporter.apple.com and provide as much detail about the use case as possible.


Thanks!

Thanks for the help, TidBits. I think I'm still missing one last piece of the puzzle here.


I am able to detect when the AVPlayerVC comes into focus from the didUpdateFocusInContext method of it's container view, but is there a way to tell from the parent view controller's didUpdateFocusInContext method which view of the AVPlayerVC has focus?


I tried testing the focus of both the container view, and the child AVPlayerVC's view using the code below, but neither registered when I gave focus to the AVPlayerVC:


        if videoPlayerContainerView.focused {
            print("video player container focused!")
        }
       
        if let avpVC = self.childViewControllers.first as? AVPlayerViewController where avpVC.view.focused {
                print("AVPVIEW IS FOCUSED")
        }


Thanks again!

AVPlayerViewController as a child VC - focus help
 
 
Q