21

I have a video which I want to download from a server and stream it in a fixed view. I've set a UIView in my storyboard with fixed constraints, and here is what I've done in code:

@IBOutlet weak var videoView: UIView!
var player: AVPlayer!
var avpController = AVPlayerViewController()

And in my viewDidLoad I've done this:

let url = URL(string:myURL)
player = AVPlayer(url: url!)

avpController.player = player
avpController.videoGravity = AVLayerVideoGravity.resizeAspect.rawValue
self.addChildViewController(avpController)
avpController.view.frame = videoView.frame
self.containerView.addSubview(avpController.view)
videoView.layer.masksToBounds = true

My problem is my video is not with the size that I've set to videoView and in every device my video is in a different size. In some devices, the video height is larger than the height that I've set and it overlays the items that I have below videoView. How can I play video in a view in a right way?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Azin Nilchi
  • 849
  • 1
  • 10
  • 24
  • 2
    Possible duplicate of [Embedding a local video into a UIView](https://stackoverflow.com/questions/45886278/embedding-a-local-video-into-a-uiview) – SPatel Jun 02 '18 at 05:58
  • 1
    @SPatel the link you mentioned, create a view programmatically, and my problem is that my video is not fit to my UIView. and I couldn't find my answer in your link, sorry :( – Azin Nilchi Jun 02 '18 at 06:09

5 Answers5

31

If you want to use AVPlayerViewController:

let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
    playerViewController.player!.play()
}

For AVPlayer:

let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
Jay
  • 686
  • 1
  • 4
  • 16
  • I think you didn't get my question ! the first way, is just presenting the video and it not in a frame and opens another view!, and the second way, is not working properly! test it in different device! – Azin Nilchi Jun 02 '18 at 08:13
  • 1
    The AVPlayer part worked flawlessly for me, just had to change the frame to where i wanted it. Thanks! – Pointblaster Apr 03 '19 at 13:20
15

the only way to add a video to a UIView with fixed constraints in storyboard, was this :

let url = URL(string:myURL)

player = AVPlayer(url: url!)

avpController.player = player

avpController.view.frame.size.height = videoView.frame.size.height

avpController.view.frame.size.width = videoView.frame.size.width

self.videoView.addSubview(avpController.view)

I hope other can use this! :)

Ashish Chauhan
  • 1,316
  • 15
  • 22
Azin Nilchi
  • 849
  • 1
  • 10
  • 24
12

You can try to use AVPlayerLayer in a AVPlayer.

      let player = AVPlayer(url: video) // your video url
      let playerLayer = AVPlayerLayer(player: player)
      playerLayer.frame = videoView.bounds
      videoView.layer.addSublayer(playerLayer)
      player.play()
  • thank you for your answer, it worked on iphone X, but in iphone 5s and even 8 plus, the frame is not what I've set in storyboard! another thing is that the player control is not showing anymore :( – Azin Nilchi Jun 02 '18 at 06:17
2

This is how you can add and remove player in uiview.

var player: AVPlayer?
var playerController: AVPlayerViewController?
func removePlayer() {
    player?.pause()
    player = nil
    playerController?.player?.pause()
    playerController?.player = nil
    if let view = playerController?.view {
        videoView.willRemoveSubview(view)
    }
    playerController?.view.removeFromSuperview()
    playerController = nil
}

func setVideo(url: URL) {
    removePlayer()
    player = AVPlayer(url: url)
    playerController = AVPlayerViewController()
    playerController?.player = player
    self.videoView.addSubview((playerController?.view)!)
    playerController?.view.frame = CGRect(x: 0, y: 0, width: self.videoView.bounds.width, height: self.videoView.bounds.height)
   // player.play()
}
Ali Aqdas
  • 29
  • 3
  • This is not the proper way to add a child view controller to a parent. And it's the wrong way to use a `AVPlayerViewController`. – HangarRash Apr 27 '23 at 01:06
-1
import AVKit
import AVFoundation
import UIKit
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        let player = AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "shiv", ofType: "mp4")!))
    
        let vc = AVPlayerViewController()
        vc.player = player
        present(vc, animated: true)
    }
}
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 02 '23 at 20:47