在Swift 3/4中单击按钮时视频无法打开?

时间:2017-07-28 21:13:12

标签: ios swift

我正在尝试构建一个应用程序,其中有一个按钮,当您单击按钮时,它应该打开视频。但由于某种原因,按钮被点击但没有打开视频。任何人都可以检查,我做错了什么。该视频位于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()




}

2 个答案:

答案 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")