0

I am trying to build an app where I have a button and when you click on a button it should open the video. But for some reason, the button is being tapped but not opening the video. Can any one check, what I have done wrong. The video is in my Folder Directory in Xcode.

Code:

   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 Answers2

1

I got it to solved by doing and using the 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()
    }
}
0

Try this as url:

Bundle.main.url(forResource: "Promo", withExtension: ".mp4")
Christoph
  • 702
  • 6
  • 16
  • I tried your method with this: `let videoURL = Bundle.main.url(forResource: "Promo", withExtension: ".mp4")` I get a `fatal error: unexpectedly found nil while unwrapping an Optional val` error when I click on the button –  Jul 28 '17 at 21:23
  • Have you added your app as target to your video? (Select the video, on the right hand side under Target Membership check your app) – Christoph Jul 28 '17 at 21:24
  • Nevermind it worked. But, how can I make it full screen ? it shows it in the middle of the app. –  Jul 28 '17 at 21:25
  • Even in landscape mode? – Christoph Jul 28 '17 at 21:28
  • I disable the landscape mode –  Jul 28 '17 at 21:29
  • and also the video ends, and not disappears from the view? –  Jul 28 '17 at 21:29
  • That is the default behaviour. Please explain how it's not full screen. Do you have borders all around the video? – Christoph Jul 28 '17 at 21:30
  • So, when you look at my code above: It is when you click on the button, it shows the full-screen ios video player, but it is not showing in my case, it is showing only the video in the middle and no I dont have borders –  Jul 28 '17 at 21:33
  • Try this: playerLayer.videoGravity = .resizeAspectFill – Christoph Jul 28 '17 at 21:36
  • Ok, it works. But why it does not show the default play and pause controllers. something like this: http://imgur.com/a/2pr3W –  Jul 28 '17 at 21:38
  • You'll need to use an AVKit Player View Controller for that. You want me to update my answer to show you how it's done? – Christoph Jul 28 '17 at 21:40
  • Actually, refer to this: https://stackoverflow.com/questions/25348877/how-to-play-a-local-video-with-swift – Christoph Jul 28 '17 at 21:42
  • check my answered question –  Jul 28 '17 at 21:44
  • Just do player.play(), looks cleaner. Otherwise, all good. :) – Christoph Jul 28 '17 at 21:49
  • @Christoph don't tell people to accept your answer. If it was helpful to the OP, they will upvote and accept. Your comments add unnecessary noise. – JAL Jul 28 '17 at 21:58
  • @JAL The reason why I was asking is because OP got off topic from his initial question. Sorry for the noise. – Christoph Jul 28 '17 at 22:01