Search code examples
iosswiftmpmovieplayercontrollerlandscape

MPMoviePlayerController - Full Screen | Swift


I am currently trying to implement a MPMoviePlayerController in my Swift app. When the video is loaded (by clicking a button on previous view controller) I would like the video to go in to full screen mode. With my current code this works perfectly in portrait but as soon as the video is rotated to landscape it only takes up half the screen. Is there some way I can get around this issue?

Here is my code:

 override func viewDidLoad() {
  super.viewDidLoad()

  var url:NSURL = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")

  moviePlayer = MPMoviePlayerController(contentURL: url)
  moviePlayer.view.frame = CGRect(x: 20, y: 100, width: 200, height: 150)

  self.view.addSubview(moviePlayer.view)
  moviePlayer.fullscreen = true

  moviePlayer.controlStyle = MPMovieControlStyle.Embedded}

And here are a few screenshots to help explain what I mean:

Portrait working great - want it like this for landscape

landscape - cuts off the picture, seems to be trying to fit the portrait view on to the landscape view controller. Weird O_o


Solution

  • The view isn't resizing because your code never tells it to.

    If you want it to resize on rotation, you need to resize the MPMoviePlayerController's view when the container view changes due to rotation. (Either set the frame in viewWillLayoutSubviews or use autolayout to constraint the MPMoviePlayerController's view to the presenting controller's view.)

    You can also use MPMoviePlayerViewController instead of MPMoviePlayerController. From the docs:

    Unlike using an MPMoviePlayerController object on its own to present a movie immediately, you can incorporate a movie player view controller wherever you would normally use a view controller.

    You can use it like this:

    class MainViewController : UIViewController {
        var movieViewController : MPMoviePlayerViewController?
    
        override func viewDidLoad() {
            var url = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!
            movieViewController = MPMoviePlayerViewController(contentURL: url)
            movieViewController?.moviePlayer.fullscreen = true
            movieViewController?.moviePlayer.controlStyle = .Embedded
        }
    
        override func viewDidAppear(animated: Bool) {
            self.presentMoviePlayerViewControllerAnimated(movieViewController)
        }
    }