Manny
Manny

Reputation: 3

Playing Video automatically on a View Controller without a button

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)
    }


        }

Upvotes: 0

Views: 937

Answers (2)

Athul Sethu
Athul Sethu

Reputation: 447

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()
}

Upvotes: 1

Thilina Chamath Hewagama
Thilina Chamath Hewagama

Reputation: 9040

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()

}

Upvotes: 0

Related Questions