在容器视图中播放本地视频 Swift 4

Posted

技术标签:

【中文标题】在容器视图中播放本地视频 Swift 4【英文标题】:Playing a local Video in a container view Swift 4 【发布时间】:2018-03-05 09:25:23 【问题描述】:

我正在尝试在名为 AVPlayerViewController 的容器视图中播放视频,但是当您单击播放器视图播放视频时,它不会播放。有没有什么办法解决这一问题?这是我用来尝试播放视频的代码:

import UIKit
import AVKit

class IntroductionViewController: UIViewController 

    var avPlayer: AVPlayer!

    override func viewDidLoad() 
        super.viewDidLoad()
    

    func playVideo() 

        let filepath: String? = Bundle.main.path(forResource: "Video", ofType: "mp4")
        let fileURL = URL.init(fileURLWithPath: filepath!)

        avPlayer = AVPlayer(url: fileURL)
        let avPlayerController = AVPlayerViewController()
        avPlayerController.player = avPlayer
        avPlayerController.showsPlaybackControls = true
        avPlayerController.view.frame = CGRect(x: 44, y: 128, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)

        // Turn on video controlls 
          avPlayerController.showsPlaybackControls = true

        // play video

        avPlayerController.player?.play()
        self.view.addSubview(avPlayerController.view)

    

【问题讨论】:

任何崩溃日志? 没有崩溃日志。视频无法播放,但应用仍在运行。 你检查过***.com/a/25349246/6080920 我检查过了,只要你在应用程序的那个部分,它就会播放视频。当我点击视图控制器时,我需要播放视频 你能告诉我你在哪里打电话给playVideo()吗? 【参考方案1】:

您的代码中有一些概念性错误;如果您希望您的 IntroductionViewController 封装视频,则应将 AVPlayerViewController 添加为子项:

import UIKit
import AVKit

class IntroductionViewController: UIViewController 

    var avPlayer: AVPlayer!

    override func viewDidLoad() 
        super.viewDidLoad()
        playVideo()
    

    func playVideo() 

        let filepath: String? = Bundle.main.path(forResource: "videoplayback", ofType: "mp4")
        let fileURL = URL.init(fileURLWithPath: filepath!)

        avPlayer = AVPlayer(url: fileURL)
        let avPlayerController = AVPlayerViewController()
        avPlayerController.player = avPlayer
        avPlayerController.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)

        // Turn on video controlls
        avPlayerController.showsPlaybackControls = true

        // play video
        avPlayerController.player?.play()
        self.view.addSubview(avPlayerController.view)
        self.addChild(avPlayerController)
    

但是您的 IntroductionViewController 也可以扩展 AVPlayerViewController:

import UIKit
import AVKit

class AVViewController: AVPlayerViewController 

    var avPlayer: AVPlayer!

    override func viewDidLoad() 
        super.viewDidLoad()

        setupAndPlay()
    

    func setupAndPlay() 
        let filepath: String? = Bundle.main.path(forResource: "videoplayback", ofType: "mp4")
        let fileURL = URL.init(fileURLWithPath: filepath!)

        // Setup AVPlayer
        avPlayer = AVPlayer(url: fileURL)
        self.player = avPlayer
        self.view.frame = CGRect(x: 44, y: 128, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)

        // Turn on video controlls
        self.showsPlaybackControls = true

        // play video
        self.player?.play()
    

选择你的方式:)

【讨论】:

你不应该继承 AVPlayerViewController。 “不要继承 AVPlayerViewController。不支持覆盖此类的方法,并导致未定义的行为。”根据文档:developer.apple.com/documentation/avkit/avplayerviewcontroller

以上是关于在容器视图中播放本地视频 Swift 4的主要内容,如果未能解决你的问题,请参考以下文章

swift 3 - 旋转到横向时全屏播放视频

Swift 表格视图,在表格视图单元格中播放视频,内存泄漏

如何使用 Swift 播放本地视频?

从收藏视图切换视频时,Swift 播放器冻结但音频仍然在后台运行

在 web 视图中播放应用本地视频 (.mp4)

在旋转ios swift时仅在uiviewcontroller中制作视频播放器景观