5

I'm playing Video in my Swift app (iOS 9) with the following code:

        let fileUrl = NSBundle.mainBundle().URLForResource("welcome", withExtension: ".mp4")
        videoItem =  AVPlayerItem(URL: fileUrl!)
        videoPlayer = AVPlayer(playerItem: videoItem)
        videoPlayer.addObserver(self, forKeyPath: "rate", options: [.New], context: nil)

        videoPlayerController.player = videoPlayer
        videoPlayerController.showsPlaybackControls = false

        videoPlayerController.view.frame = CGRectMake(0, 0, playerView.frame.size.width, playerView.frame.size.height)
        playerView.insertSubview(videoPlayerController.view, belowSubview: videoCoverView)

        videoPlayerController.willMoveToParentViewController(self)
        addChildViewController(videoPlayerController)
        videoPlayerController.didMoveToParentViewController(self)

        playerView.bringSubviewToFront(btnPlay)

        let asset = AVAsset(URL: fileUrl!)
        let imageGenerator = AVAssetImageGenerator(asset: asset)
        let time = CMTime(seconds: asset.duration.seconds/2.0, preferredTimescale: 1)

        do {
            let imageRef = try imageGenerator.copyCGImageAtTime(time, actualTime: nil)
            let coverImage = UIImage(CGImage: imageRef)
            videoCoverView.image = coverImage
        } catch {

        }

videoPlayerController is an instance of AVPlayerViewController. playerView is added through storyboard and has Autolayout setup on it. I face Autloayout issues once the player controls of AVPlayerViewController show up and also when I try to go in full screen mode through player controls. Here is the log that gets printed.

2016-05-07 15:18:19.325 VoxTrain[2085:66307] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7fb6a2a3f2c0 h=-&- v=-&- _UIBackdropContentView:0x7fb6a2a02310.width == _UIBackdropView:0x7fb6a053bf90.width>",
    "<NSLayoutConstraint:0x7fb6a2992800 H:|-(14)-[UILabel:0x7fb6a2991220'Hi-Speed Scrubbing']   (Names: '|':_UIBackdropContentView:0x7fb6a2a02310 )>",
    "<NSLayoutConstraint:0x7fb6a2992850 H:[UILabel:0x7fb6a2991220'Hi-Speed Scrubbing']-(14)-|   (Names: '|':_UIBackdropContentView:0x7fb6a2a02310 )>",
    "<NSLayoutConstraint:0x7fb6a29649a0 H:|-(0)-[_UIBackdropView:0x7fb6a053bf90]   (Names: '|':UIView:0x7fb6a0549220 )>",
    "<NSLayoutConstraint:0x7fb6a298b8a0 H:[_UIBackdropView:0x7fb6a053bf90]-(0)-|   (Names: '|':UIView:0x7fb6a0549220 )>",
    "<NSLayoutConstraint:0x7fb6a296f440 H:|-(0)-[UIView:0x7fb6a0549220]   (Names: '|':AVAlphaUpdatingView:0x7fb6a053dae0 )>",
    "<NSLayoutConstraint:0x7fb6a296f490 H:[UIView:0x7fb6a0549220]-(0)-|   (Names: '|':AVAlphaUpdatingView:0x7fb6a053dae0 )>",
    "<NSLayoutConstraint:0x7fb6a2a40cc0 'UIView-Encapsulated-Layout-Width' H:[AVAlphaUpdatingView:0x7fb6a053dae0(0)]>"
)

After the initial approach, I tried to set autolayout on videoPlayerController's view with setting translatesAutoresizingMaskIntoConstraints to false. But the issue still persists. How can I fix these issues?

MGY
  • 7,245
  • 5
  • 41
  • 74
Abid Hussain
  • 1,529
  • 1
  • 15
  • 33

2 Answers2

0

Use this code,

viewDidLoad method:

        let type : String! = "mp4"
        let targetURL : String? = NSBundle.mainBundle().pathForResource("Welcome", ofType: type)

        let videoURL = NSURL(fileURLWithPath:targetURL!)


        let player = AVPlayer(URL: videoURL)
        let playerController = AVPlayerViewController()

        playerController.player = player
        self.addChildViewController(playerController)
        self.playView.addSubview(playerController.view)
        playerController.view.frame = playView.bounds

        player.play()

And my Storyboard View is give below,

enter image description here

I have create the UIView, then set the top,left, right and Height that solve. its working for me

Hope its helpful

Iyyappan Ravi
  • 3,205
  • 2
  • 16
  • 30
  • Nope. I'm already doing all of these things only in a different order. Only difference is that my PlayerView's height is proportional to ViewController's view. The video runs smoothly. Only problem occurs when you try to show native controls of AVPlayerViewCOntroller by setting its showsPlaybackControls property true. And now If you try to go in full screen by using these controls then Autolayout log gets printed on console. – Abid Hussain May 07 '16 at 11:16
-1

The problem is that you trying to set showsPlaybackControls = true when your AVPlayerController is onscreen. Doing so creates system UI elements, which leads to Auto Layout issues.

Docs are very specific about this point:

Do not use this property to change the visibility of the playback controls while the player view controller is onscreen, because doing so creates or destroys UI elements.

Link: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVPlayerViewController_Class/#//apple_ref/occ/instp/AVPlayerViewController/showsPlaybackControls

Vladimir K
  • 1,382
  • 1
  • 12
  • 27
  • @Abid I am facing same issue. Do you got any solution? – The iCoder Jun 22 '17 at 17:07
  • I created an AVPlayerViewController instance and added it to a view like so: moviePlayer = AVPlayerViewController.init() moviePlayer?.allowsPictureInPicturePlayback = false moviePlayer?.showsPlaybackControls = true moviePlayer?.player = AVPlayer.init(url: item!.mediaURL!) moviePlayer?.view.translatesAutoresizingMaskIntoConstraints = false contentView.addSubview(moviePlayer!.view) moviePlayer!.view.pinAllAnchors(to: contentView) ``` I still see auto layout issues when I go fullscreen and back. – Cezar Signori Jul 06 '17 at 10:02