我试图建立一个应用程序,我有一个按钮,当你点击一个按钮,它应该打开视频。但由于某些原因,按钮被点击但没有打开视频。有人能查一下我做错了什么吗。视频在Xcode的“我的文件夹”目录中。
代码:
var playerview = AVPlayer()
var playerviewcontroller = AVPlayerViewController()
@IBAction func playBtnTapped(_ sender: Any) {
//print("btn tapped")
let videoURL = URL(string: "Promo.mp4")
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
}
最佳答案
我通过做和使用AVPlayerViewController
来解决这个问题:
let videoURL = Bundle.main.url(forResource: "Promo", withExtension: ".mp4")
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
}
关于ios - 在Swift 3/4中单击按钮时视频无法打开?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45382495/