4

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.

Marmoy
  • 8,009
  • 7
  • 46
  • 74
Ruchi
  • 411
  • 4
  • 13

2 Answers2

9

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

Piyuesh
  • 1,066
  • 1
  • 9
  • 18
7

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

David Cordero
  • 770
  • 6
  • 16
  • Hey thanks for clearance David. Is that any way I can show controls for avplayer when space is less than full screen? – Ruchi Mar 27 '17 at 03:41
  • @Ruchi yes you can do it adding your custom views over an instance of AVPlayer, getting info out of AVPlayer properties, but it is not possible making use of a native AVPlayerViewController. – David Cordero Mar 27 '17 at 07:38
  • Ok. Do you know any third party library or framework for that? I searched but couldn't get a single one. – Ruchi Mar 27 '17 at 11:58