Воспроизведение видео автоматически на контроллере вида без кнопки - PullRequest
0 голосов
/ 08 апреля 2020

Может кто-нибудь показать мне, что я делаю не так? Я все исправил и протестировал код, но при каждом запуске приложения на моем устройстве видео не воспроизводится. Показывается состояние паузы. Я не знаю, почему видео не будет воспроизводиться автоматически.

Вот мой код:

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


        }

Ответы [ 2 ]

1 голос
/ 08 апреля 2020

Поскольку вы хотите автоматически запустить видео и l oop, вы должны использовать AVPlayerLooper, а также вам следует вызвать play() метод объекта player.

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

}
0 голосов
/ 08 апреля 2020

просто добавьте self.player?.play() в конце. Как

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()
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...