0

I am trying to centre the video player(medialPlayer.view) inside the UIView(videoPlayerView). But for some reason it is not aligning at the centre. Can anyone tell me how to do it and explain what is wrong in the code below? enter image description here

// Code is below

 @IBOutlet var videoPlayerView: UIView!

var moviePlayer:MPMoviePlayerController!

override func viewDidLoad() {
    super.viewDidLoad()

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

    moviePlayer = MPMoviePlayerController(contentURL: url)
    let frameWidth = self.videoPlayerView.frame.size.width
    let frameHeight = self.videoPlayerView.frame.size.height

    moviePlayer.view.frame = CGRect(x: 0, y: 0, width: frameWidth, height: frameHeight)


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

    moviePlayer.controlStyle = MPMovieControlStyle.Embedded

}
Michal
  • 15,429
  • 10
  • 73
  • 104
NS1518
  • 838
  • 1
  • 8
  • 23

2 Answers2

0

In viewWillAppear try centering the player view :

moviePlayer.view.center = videoPlayerView.center

See if that changes anything.

Teo
  • 3,394
  • 11
  • 43
  • 73
  • Double check what you wrote because that error will not appear if you type what I wrote above. – Teo Jun 05 '15 at 13:55
  • I am getting this error: UIViewAlertForUnsatisfiableConstraints: The app just crashes as soon as the view loads. – NS1518 Jun 05 '15 at 13:59
  • If is remove: moviePlayer.view.frame = CGRect(x: 0, y: 0, width: frameWidth, height: frameHeight) and just keep moviePlayer.view.center = videoPlayerView.center -> App crash. if I add moviePlayer.view.center = videoPlayerView.center inside viewWillAppear the mediaPlayer.View is outside the videoPlayerView from bottom and is at x=0. So still not centred. – NS1518 Jun 05 '15 at 14:07
0

After few trial an errors and some searching SO, i finally worked it out. Posting the code so that others can make use of it. I am using the code from this SO link SWIFT | Adding constraints programmatically.

My detail code is as below:

 @IBOutlet var videoPlayerView: UIView!
    var moviePlayer:MPMoviePlayerController!

override func viewDidLoad() {
    super.viewDidLoad()

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

    moviePlayer = MPMoviePlayerController(contentURL: url)
    let frameWidth = self.videoPlayerView.frame.size.width
    let frameHeight = self.videoPlayerView.frame.size.height

    //moviePlayer.view.frame = CGRect(x: 24, y: 18, width: frameWidth, height: frameHeight)

    self.videoPlayerView.addSubview(moviePlayer.view)
    moviePlayer.fullscreen = true
    moviePlayer.controlStyle = MPMovieControlStyle.Embedded

    // Pure Visual Format Language style (https://stackoverflow.com/questions/26180822/swift-adding-constraints-programmatically)
    (moviePlayer.view).setTranslatesAutoresizingMaskIntoConstraints(false)
    let views = ["view": videoPlayerView, "videoView": moviePlayer.view]

    var constH = NSLayoutConstraint.constraintsWithVisualFormat("H:[view]-(<=0)-[videoView(\(frameWidth))]", options: .AlignAllCenterY, metrics: nil, views: views)
    view.addConstraints(constH)
    var constW = NSLayoutConstraint.constraintsWithVisualFormat("V:[view]-(<=0)-[videoView(\(frameHeight))]", options: .AlignAllCenterX, metrics: nil, views: views)
    view.addConstraints(constW)

Note: I am using UIViews width (w) and height (h) to calculate video's w&h. If you want to use standard size w&h just change frameWidth & frameHeight inside constW & constH to your own standard size and it will work! Good luck!

Community
  • 1
  • 1
NS1518
  • 838
  • 1
  • 8
  • 23