3

I was following this video: https://developer.apple.com/videos/play/wwdc2014-503/

And so I made a quick demo and tried add a container View and changing the subview to an avplayer like they showed. Then i added the code, although in swift form.

The difference is in the constructor for the AVPlayer in the prepareForSegue function i wrote the following:

playerViewController.player = AVPlayer(URL: 
    NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!)

The video is very wide and what happens is half the video player expands past the screen ios emulator

I tried playerViewController.view.sizeToFit() and on the properties for the video controller i've tried all the different videoGravity settings

(disclaimer: I am very bad with IDEs. I generally avoid them, but with iOS it appears impossible)

All of the code:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showMovie" {
        NSLog("im in!!");
        //self.view.translatesAutoresizingMaskIntoConstraints = false
        let playerViewController = segue.destinationViewController as! AVPlayerViewController
  //playerViewController.view.translatesAutoresizingMaskIntoConstraints = false;

        //setup player view controller for movie
        playerViewController.player = AVPlayer(URL: NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!)

        playerViewController.view.sizeToFit()

        //playerViewController.showsPlaybackControls = true
        //playerViewController.player?.play();
    }
}

Here's a screenshot of the storyboard. I have no idea how that could help.

enter image description here

Sophie McCarrell
  • 2,831
  • 8
  • 31
  • 64
  • 2
    Please post a picture of your storyboard. This looks like a storyboard issue. – Oz Solomon Feb 17 '16 at 17:58
  • Please add another screenshot: (1) Click on the Container View. (2) From the View menu select Utilities -> Show Size Inspector. Take a screenshot that shows the container view and the inspector. My guess is that you're missing some constraints. – Oz Solomon Feb 24 '16 at 21:31

1 Answers1

0

I would try to pass the URL of the video only in segue. Since in the prepareForSegue the view of your view controller may not be fully initialized and fitted to screen with autolayout.

So in your actual viewController :

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showMovie" {
        NSLog("im in!!");
        //self.view.translatesAutoresizingMaskIntoConstraints = false
        let playerViewController = segue.destinationViewController as! AVPlayerViewController

    //setup player view controller for movie
    playerViewController.movieURL= "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v"

}

And in the viewWillAppear of the AVPlayerViewController, use thecode

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated);
        //setup player view controller for movie
        playerViewController.player = AVPlayer(URL: NSURL(string: self.movieURL)!)

        playerViewController.view.sizeToFit()
}
cdescours
  • 6,004
  • 3
  • 24
  • 30
  • There was a lot that was unclear about your answer, but I think i filled the many gaps and it still doesn't work. In fact the video doesn't show at all any more. I create a new cocoa UI class and set the custom class for the video's view controller. Then i created the viewWillAppear and changed the playerViewController to "self". Also I merely copy pasted the movie URL as this class wouldn't have the movie URL. It still didn't work however. – Sophie McCarrell Feb 24 '16 at 21:11