2

I have view hierarchy like following:

UIViewController
    UIView (Top most view)
        UIView (ContainerView)
            UIView (MoviePlayer.swift) (with layer as AVPlayerLayer)

The MoviePlayer file is posted here. Now I want to make the MoviePlayer view go full screen. The current code does not work good as the view is constrained inside another view. I tried making y co-ordinate negative but that does not help much. Somehow that approach also messes with the click events of buttons. Another approach I thought was to show a new view controller with the MoviePlayer view common between the two or something like that I don't know how to do that. I basically want to do that like the facebook iOS app shows in table view cells.

I don't want to use MPMoviePlayerController, it's already deprecated and had used it previously which had many problem and didn't allow customisations.

This is link to demo code on github where I tried a similar setup. I cannot share my real code. Clicking on fullscreen shows the problem. I actually have to also implement a similar video player in UITableViewCell.

meteors
  • 1,747
  • 3
  • 20
  • 40

1 Answers1

1

You can try apply some CGAffineTransform to your layer.

I face similar issue, and here is what work for me: I resize container with controls for player by changing it's constraint's constants and apply transform to player layer. Here example of method that create CGAffineTransform from CGRect.

func CGAffineTransformFromRect(sourceRect: CGRect, toRect finalRect:CGRect) -> CGAffineTransform {
    var transform = CGAffineTransformIdentity
    transform = CGAffineTransformTranslate(transform, -(CGRectGetMidX(sourceRect) - CGRectGetMidX(finalRect)), -(CGRectGetMidY(sourceRect) - CGRectGetMidY(finalRect)))
    transform = CGAffineTransformScale(transform, finalRect.size.width / sourceRect.size.width, finalRect.size.height / sourceRect.size.height)
    return transform
}
rkyr
  • 3,131
  • 2
  • 23
  • 38
  • So should I change the container size of the view containing MoviePlayer view and apply CGAffineTransform to MoviePlayer view – meteors Sep 11 '15 at 13:04
  • @meteors, yes. Try it and reply is it help you. – rkyr Sep 11 '15 at 13:11
  • @RK it tried doing so. But that actually stretched the entire view. Actually previously when I changed the view frame of MoviePlayer the AVPlayerLayer automatically occupied the available frame if the video is in portrait mode. I've attached link to demo github project in the question. – meteors Sep 11 '15 at 13:24
  • @meteors, sorry. I didn't understand you correctly. [Here](https://www.dropbox.com/s/pm03n0jr6389t8j/VideoPlayer-master.zip?dl=0) I post updated project from your github. I'm not posting it here because there is a lot of code to copy-paste. Idea is next: create container in IB, add your player to this container and apply constraints to this player (programmatically). When you need to resize your player you simple change constants of player's constraints. Also I include comments in changed lines. – rkyr Sep 11 '15 at 13:45
  • Thanks @R.K. I'll check and let you know. – meteors Sep 11 '15 at 14:14