swift - 视频播放(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")
}
解决方案
目前您正在将一个函数插入另一个函数,它应该是
func forbutton(name : String, type : String) {
playVideo(name: name, type: type)
}
如果您想在视图出现时播放它
func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
forbutton(name: "fatB", type: "mp4")
}
推荐阅读
- javascript - 如何从匿名函数中正确调用外部类方法
- ruby - 为什么 Inspec 在检查 aws_elbs 端口时返回“expected [] to include”?
- c - 从字符串中提取整数并将它们保存到链表中
- laravel - 从 Laravel Nova 中的另一个获取字段值
- scala - 使用大小写匹配优化scala中列表的比较
- android - Firebase 项目数量和可用性
- maven - TestNG NPE:使用surefire运行时,org.testng.xml.XmlSuite.getAllParameters(XmlSuite.java:434)出现间歇性NullPointerException
- excel - 循环遍历多行以将变量放入 API 调用中
- selenium - 通过 Jenkins 中的 run.bat 文件运行我的 git 测试时,在类路径异常中找不到类
- c# - C#编译器如何保证转换成功?