0

I'm trying to set the video frame of an AVPlayer so that it takes like 75% of the screen size and let the background view visible using transparency.

However, I couldn't manage to set the size of my new VC, it always takes the whole width.

Here is my code :

import AVKit
import AVFoundation

class VideoPlayer {

// ----------------------------------------------------------------------------------
// MARK: - Properties
// ----------------------------------------------------------------------------------
var videoName = "here video url"
let playerViewController = AVPlayerViewController()

// ----------------------------------------------------------------------------------
// MARK: - Setup
// ----------------------------------------------------------------------------------
func setupAndPlayVideo(sourceViewController: UIViewController) {

    // Get video URL
    let videoURL = urlForFile(videoName)
    let player = AVPlayer(URL: videoURL!)

    // Grey transparent background
    playerViewController.view.backgroundColor = UIColor(red: 0.4, green: 0.4, blue: 0.4, alpha: 0.5)

    // Prevent user from pausing/dismissign the video
    playerViewController.showsPlaybackControls = false

    // Present the video player VC and play video
    playerViewController.player = player

    // This does not change anything (sample hard coded values)
    playerViewController.view.frame = CGRectMake(0, 0, 300, 300)

    sourceViewController.presentViewController(playerViewController, animated: true) {
        self.playerViewController.player!.play()
    }
}
}

EDIT

So I decided to change my strategy and add an AVPlayerViewController in my storyboard.

Here is the code I use to play my video :

    // Game over
    case PapooSegue.GameEndSegue.rawValue:

        // Get photo VC
        let videoVC = segue.destinationViewController as! AVPlayerViewController

        // Get video URL and provide it to the player
        let videoName = papooBrain!.getGameOverVideoName()
        let videoURL  = urlForFile(videoName)
        videoVC.player = AVPlayer(URL: videoURL!)

        // Set grey/transparent background
        videoVC.view.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.6)

        // Play video
        videoVC.player?.play()

The problem is still the same. I tried to play with the AVPlayerLayer as suggested in this post, but I ended up with 2 videos playing :

  • The one that is stretched by videoGravity I guess
  • The one with a custom frame

I don't know how I can have only one on these playing with the appropriate size.

Community
  • 1
  • 1
H4Hugo
  • 2,570
  • 3
  • 16
  • 32

1 Answers1

0

Correct me if I'm wrong but it looks like you're using the VideoPlayer class in a different view controller. You should be able to control the frame from that VC, not the VideoPlayer class. So if you go to the VC containing the VideoPlayer create the videoPlayer var -

Example: var videoPlayer : VideoPlayer! 

then in your viewdidload you should be able to call your function. by saying

videoPlayer.setUpAndPlayVideo

Hope this helps.

Andrew Cook
  • 116
  • 1
  • 8