0

Can anyone show me what I am doing wrong? I have put everything right and tested the code, but every time I run the application on my device the video does not play. It is being shown a paused state. I don't know why the video will not play automatically.

Here is my code:

import UIKit
import AVFoundation

class ViewController: UIViewController {

    var player: AVPlayer?

    @IBOutlet weak var videoViewContainer: UIView!

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

    func initializeVideoPlayerWithVideo() {

        // get the path string for the video from assets
        let videoString:String? = Bundle.main.path(forResource: "BackgroundAppVideo", ofType: "mov")
        guard let unwrappedVideoPath = videoString else {return}

        // convert the path string to a url
        let videoUrl = URL(fileURLWithPath: unwrappedVideoPath)

        // initialize the video player with the url
        self.player = AVPlayer(url: videoUrl)

        // create a video layer for the player
        let layer: AVPlayerLayer = AVPlayerLayer(player: player)

        // make the layer the same size as the container view
        layer.frame = videoViewContainer.bounds

        // make the video fill the layer as much as possible while keeping its aspect size
        layer.videoGravity = AVLayerVideoGravity.resizeAspectFill

        // add the layer to the container view
        videoViewContainer.layer.addSublayer(layer)
    }


        }
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Manny
  • 3
  • 1
  • 1
    You probably want `viewDidAppear` but as it's potentially called multiple times you'll want a flag to ensure you only show the video the first time. – trojanfoe Apr 08 '20 at 07:40

2 Answers2

1

just add self.player?.play() at the end. As

func initializeVideoPlayerWithVideo() {

    // get the path string for the video from assets
    let videoString:String? = Bundle.main.path(forResource: "BackgroundAppVideo", ofType: "mov")
    guard let unwrappedVideoPath = videoString else {return}

    // convert the path string to a url
    let videoUrl = URL(fileURLWithPath: unwrappedVideoPath)

    // initialize the video player with the url
    self.player = AVPlayer(url: videoUrl)

    // create a video layer for the player
    let layer: AVPlayerLayer = AVPlayerLayer(player: player)

    // make the layer the same size as the container view
    layer.frame = videoViewContainer.bounds

    // make the video fill the layer as much as possible while keeping its aspect size
    layer.videoGravity = AVLayerVideoGravity.resizeAspectFill

    // add the layer to the container view
    videoViewContainer.layer.addSublayer(layer)
    self.player?.play()
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Athul Sethu
  • 387
  • 2
  • 4
  • 18
  • Yes this worked. I just tried it and the video plays. How would I have the video restart after it is finished playing? – Manny Apr 08 '20 at 07:46
0

Since you want to automatically start the video and loop it, You have to use AVPlayerLooper and also, you should call play() method of the player object.

var player: AVQueuePlayer?
var videoLooper: AVPlayerLooper?
@IBOutlet weak var videoViewContainer:UIView!

override func viewDidLoad() {
    super.viewDidLoad()
    self.initializeVideoPlayerWithVideo()

}

func initializeVideoPlayerWithVideo() {

    // get the path string for the video from assets
    let videoString:String? = Bundle.main.path(forResource: "BackgroundAppVideo", ofType: "mov")
    guard let unwrappedVideoPath = videoString else {return}

    // convert the path string to a url
    let videoUrl = URL(fileURLWithPath: unwrappedVideoPath)

    let asset = AVAsset(url: videoUrl)
    let item = AVPlayerItem(asset: asset)

    // initialize the video player with the url
    self.player = AVQueuePlayer()

    // create a video layer for the player
    let layer: AVPlayerLayer = AVPlayerLayer(player: player)

    // make the layer the same size as the container view
    layer.frame = videoViewContainer.bounds

    // make the video fill the layer as much as possible while keeping its aspect size
    layer.videoGravity = AVLayerVideoGravity.resizeAspectFill

    // add the layer to the container view
    videoViewContainer.layer.addSublayer(layer)

    videoLooper = AVPlayerLooper(player: self.player!, templateItem: item)
    self.player?.play()

}
BDL
  • 21,052
  • 22
  • 49
  • 55
Thilina Chamath Hewagama
  • 9,039
  • 3
  • 32
  • 45