AVPlayerViewController inside a view is not showin

2020-07-26 05:01发布

问题:

I am working with apple tv. I have a UIView inside my UIViewController. I am adding AVPlayerViewController as subview of my UIView. UIView's frame is CGRect(x: 50, y: 50, width: 1344, height: 756). I am setting AVPlayerViewController frame equals to my UIView's frame.

Issue is that video plays fine in its frame but the controls like pause, play, forward etc. are hidden and not working. But when I set frame as CGRect(x: 0, y: 0, width: 1920, height: 1080), controls are visible and working.

My code is as per below:

    let url = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
    let item = AVPlayerItem(url: url!)        
    self.player = AVPlayer(playerItem: item)
    self.avplayerController = AVPlayerViewController()
    self.avplayerController.player = self.player
    self.avplayerController.view.frame = videoPreviewLayer.frame
    self.avplayerController.showsPlaybackControls = true
    self.avplayerController.requiresLinearPlayback = true

    self.addChildViewController(self.avplayerController)
    self.view.addSubview(self.avplayerController.view)

    self.avpController.player?.play()

Please help.

回答1:

This is an expected behaviour:

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.

You can read more about this on this thread: https://forums.developer.apple.com/thread/19526



回答2:

I managed to solve this using this code:

let player = AVPlayer(url: self.videoUrl!)
let avPlayerController = AVPlayerViewController()

avPlayerController.player = player;
avPlayerController.view.frame = self.videoPlayView.bounds;

self.addChildViewController(avPlayerController)
self.videoPlayView.addSubview(avPlayerController.view);

Now AVPlayerViewController shows most playback controls even as child to a uiview.

Courtesy: https://www.techotopia.com/index.php/IOS_8_Video_Playback_using_AVPlayer_and_AVPlayerViewController