0

I have an app which plays video clips on demand. This has worked well in previous versions of Xcode but I've upgraded to 8.3.3 and am having problems with the AVPlayerViewController.

The video plays and displays correctly. The control bar appears at the bottom of the view but does not respond to taps and, once faded out, does not reappear on tapping the video - unless, that is, I tap near the top left of the view.

My guess is that the actual controls are hidden in some kind of overlay which has the wrong size i.e. it does not properly overlay the whole of the video view. Is there some way to force the AVPlayerViewController to relayout?

I've tried adding:

    _playerController!.view.setNeedsLayout()
    _playerController!.view.layoutIfNeeded()

But this has no effect.

Here's my code:

override func viewDidLoad() {

    super.viewDidLoad()

    _player = AVPlayer()
    _playerController = AVPlayerViewController()
    _playerController!.showsPlaybackControls = true
    _playerController!.allowsPictureInPicturePlayback = false
    _playerController!.videoGravity = AVLayerVideoGravityResizeAspectFill
    _playerController!.player = _player
    self.addChildViewController(_playerController!)
    videoView.addSubview(_playerController!.view)
...

override func viewDidLayoutSubviews() {

    let frame = UIScreen.main.bounds        
    _vidWidth = frame.width - 256.0
    _vidHeight = _vidWidth / 854.0 * 480.0

    videoView.frame = CGRect(x: 0, y: 10.0, width: _vidWidth, height: _vidHeight)
    videoView.backgroundColor = UIColor.black

    _playerController?.view.frame = CGRect(x: 0, y: 0, width: _vidWidth, height: _vidHeight)
...

func playVideo(_ clip: Clip) {

    var videoUrl: URL? = nil

    if  clip.offlineLocation != nil && clip.state == 2 {
        _videoPath = clip.offlineLocation!
        videoUrl = URL(fileURLWithPath: _videoPath!)
    }
    else {
        _videoPath = "https://xxxx.cloudfront.net/" + clip.location
        videoUrl = URL(string: _videoPath!)
    }

    NSLog(_videoPath!)

    let asset = AVURLAsset(url:videoUrl!, options:nil)
    let playerItem = AVPlayerItem(asset: asset)

    _player!.replaceCurrentItem(with: playerItem)
    _player!.play()
}
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
JohnnyD
  • 477
  • 1
  • 4
  • 19
  • please refer below link. Are you adding a AVKit and AVFoundation framework? Also put your code in viewDidAppear(_ animated: Bool) or somewhere after instead of viewDidLoad(). https://stackoverflow.com/questions/25932570/how-to-play-video-with-avplayerviewcontroller-avkit-in-swift – Shah Nilay Jul 31 '17 at 11:53
  • I definitely have the relevant frameworks added and moving the code to viewDidAppear does not make any difference. I've also tried putting player.play() inside DispatchQueue.main.async() {... – JohnnyD Jul 31 '17 at 12:08
  • This is old, but I was adding a AVPlayerViewController to table view cells an had same problem with the controls not working. I needed to set parent view of AVPlayerViewController view .isUserInteractionEnabled = true. – Yarn Oct 30 '18 at 22:47

0 Answers0