Gerard Wilkinson
Gerard Wilkinson

Reputation: 1541

MPMoviePlayerController not staying within bounds of UIView

I am trying to add a video to a UIView container within a modal window. When I add the video it does not stay within the bounds of the UIView which it is bounded to. Here is a picture of what it currently looks like:

Current State

The grey container which you can see the right edge of is the container and here is the view controller which adds the video.

import Foundation
import MediaPlayer

class EvalInstructionsVC: UIViewController {
    private var moviePlayer : MPMoviePlayerController?

    @IBOutlet weak var video: UIView!

    func playVideo() {
        let path = NSBundle.mainBundle().pathForResource("Time to Rotate Demo", ofType:"mp4")
        let url = NSURL.fileURLWithPath(path!)
        moviePlayer = MPMoviePlayerController(contentURL: url)
        if let player = moviePlayer {
            player.view.frame = video.bounds
            player.view.center = CGPointMake(CGRectGetMidX(video.bounds), CGRectGetMidY(video.bounds))
            player.prepareToPlay()
            player.scalingMode = MPMovieScalingMode.AspectFill
            video.addSubview(player.view)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        playVideo()
    }

    @IBAction func done(sender: UIBarButtonItem) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
}

As you can see the bounds are being set and it is being added as a sub view to video UIView which is the grey box in the screenshot above.

I would appreciate any help anyone can provide.

Cheers, Gerry

Upvotes: 2

Views: 204

Answers (1)

monozok
monozok

Reputation: 605

Call player.view.frame = video.bounds later in the view controller lifecycle.

Simplest solution is to call playVideo() from viewDidAppear:

override func viewDidAppear(animated: Bool) {
    playVideo()
}

FYI, our code is so similar (based on the same example?), I think you also probably won't need to set player.view.center.

Upvotes: 2

Related Questions