【问题标题】:video playing (AVPlayer ViewController, AVkit) is not working (Does not appear)视频播放(AVPlayer ViewController、AVkit)不工作(不出现)
【发布时间】:2018-05-18 15:40:28
【问题描述】:

我搜索了几个小时,但没有找到解决方案...... 视频播放代码不起作用(本地视频不出现...) 我还尝试通过带有 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")
    }

【问题讨论】:

  • 我建议将viewDidAppear 移出forbutton 函数。
  • 我试过但是问题是这个方法不接受参数,我需要为每个按钮更改它们
  • 不,问题是您在另一个函数中有一个函数定义。这对你想做的事情不起作用

标签: swift video avfoundation local avkit


【解决方案1】:

目前你在另一个里面插入一个函数,它应该是

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

    playVideo(name: name, type: type)

}

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

func viewDidAppear(_ animated: Bool) {

    super.viewDidAppear(animated)

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

【讨论】:

  • 尽管我通过构建阶段添加了视频,但代码跳转一直向我发送“视频不可用”。你知道为什么吗?
猜你喜欢
  • 2013-07-02
  • 1970-01-01
  • 2016-10-30
  • 1970-01-01
  • 2017-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多