0

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

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
dwinnbrown
  • 3,789
  • 9
  • 35
  • 60

2 Answers2

4

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)
    }
}
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • Thanks for the reply and editing the question. If I wanted to go about doing this via the first option you listed, would I simply take out the line including the subview? thanks – dwinnbrown Apr 18 '15 at 20:20
  • @dwinnbrown I updated my answer with a clearer explanation and some sample code. Hope that helps. – Aaron Brager Apr 18 '15 at 20:30
  • Yes, I've just found it. Thanks a lots will read through it all and report back! – dwinnbrown Apr 18 '15 at 20:31
  • I've just tried that code but it doesn't work for me. I just get a blank white canvas (apart from the navigation bar). Have you got any suggestions? – dwinnbrown Apr 18 '15 at 20:55
  • @dwinnbrown It worked for me. Are you trying it on a real device, or just a simulator? – Aaron Brager Apr 18 '15 at 20:58
  • I'm trying it on the simulator as I don't currently have access to an iOS Developer Account. – dwinnbrown Apr 18 '15 at 21:02
  • Sorry just realised that because the name of the class changed, the view controller in the storyboard was not connected to it! Think you've sorted it! – dwinnbrown Apr 19 '15 at 08:13
0

For Xcode 6.4 , Swift 1.2:

SOLUTION:

I set the fullscreen mode in

viewWillLayoutSubviews()

  override func viewWillLayoutSubviews()
{
    self.moviePlayer.fullscreen = true
}

Full code:

import UIKit

import MediaPlayer

class XXXMovieC: UIViewController
{

private var moviePlayer: MPMoviePlayerController!
var movieURL: NSURL!

required init(coder aDecoder: NSCoder)
{
    super.init(coder: aDecoder)
}

override func viewWillLayoutSubviews()
{
    self.moviePlayer.fullscreen = true
}


override func viewDidLoad()
{
    super.viewDidLoad()

    setupMoviePlayer()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "movieFinishedCallback:", name: MPMoviePlayerWillExitFullscreenNotification, object: self.moviePlayer)

    self.moviePlayer.shouldAutoplay = true
    self.moviePlayer.prepareToPlay()
    self.moviePlayer.play()
}

func setupMoviePlayer()
{
    self.moviePlayer = MPMoviePlayerController(contentURL: self.movieURL)
    self.moviePlayer.view.frame = self.view.frame
    self.moviePlayer.controlStyle = MPMovieControlStyle.Embedded
    self.view.addSubview(self.moviePlayer.view)
}

func movieFinishedCallback(notification:NSNotification)
{
    self.dismissViewControllerAnimated(false, completion: { () -> Void in
        self.movieURL = nil
    })
}

}
MB_iOSDeveloper
  • 4,178
  • 4
  • 24
  • 36