首页 > 解决方案 > 视频播放(AVPlayer ViewController、AVkit)不工作(不出现)

问题描述

我搜索了几个小时,但没有找到解决方案......视频播放代码不起作用(本地视频没有出现......)我还尝试通过打开视频的打开视频和 URL 代码,它们都不起作用。希望你能找到解决办法,谢谢!

下面的代码:

import UIKit
import AVKit
import AVFoundation

class ViewController2: UIViewController {

    func forbutton(name : String, type : String)  {
        func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            playVideo(name: name, type: type)
        }
    }


    private func playVideo(name : String, type : String) {
        guard let path = Bundle.main.path(forResource: "\(name)", ofType:"\(type)") else {
            debugPrint("video not found")
            return
        }
        let player = AVPlayer(url: URL(fileURLWithPath: path))
        let playerController = AVPlayerViewController()
        playerController.player = player
        present(playerController, animated: true) {
            player.play()
        }
    }




    @IBAction func fatB(_ sender: Any) {
        forbutton(name: "fatB", type: "mp4")

    }


    @IBAction func coderB(_ sender: Any) {

        forbutton(name: "birdC", type: "mp4")
    }


    @IBAction func clashB(_ sender: Any) {

        forbutton(name: "clash", type: "mp4")
    }

标签: swiftvideoavfoundationlocalavkit

解决方案


目前您正在将一个函数插入另一个函数,它应该是

func forbutton(name : String, type : String)  {

    playVideo(name: name, type: type)

}

如果您想在视图出现时播放它

func viewDidAppear(_ animated: Bool) {

    super.viewDidAppear(animated)

    forbutton(name: "fatB", type: "mp4")
}

推荐阅读