我正在尝试构建一个应用程序,其中有一个按钮,当您单击按钮时,它应该打开视频。但由于某种原因,按钮被点击但没有打开视频。任何人都可以检查,我做错了什么。该视频位于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()
}
答案 0 :(得分:1)
我通过使用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()
}
}
答案 1 :(得分:0)
尝试将其作为网址:
Bundle.main.url(forResource: "Promo", withExtension: ".mp4")