1

I am trying to play a video in a container view called AVPlayerViewController however when you click on the player view to play the video it does not play. Is there any way to fix this? Here is the code I am using to try and play the video:

import UIKit
import AVKit

class IntroductionViewController: UIViewController {

    var avPlayer: AVPlayer!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func playVideo() {

        let filepath: String? = Bundle.main.path(forResource: "Video", ofType: "mp4")
        let fileURL = URL.init(fileURLWithPath: filepath!)

        avPlayer = AVPlayer(url: fileURL)
        let avPlayerController = AVPlayerViewController()
        avPlayerController.player = avPlayer
        avPlayerController.showsPlaybackControls = true
        avPlayerController.view.frame = CGRect(x: 44, y: 128, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)

        // Turn on video controlls 
          avPlayerController.showsPlaybackControls = true

        // play video

        avPlayerController.player?.play()
        self.view.addSubview(avPlayerController.view)

    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
T.Shannon
  • 11
  • 1
  • 3

1 Answers1

0

There are some conceptual errors in your code; if you want that your IntroductionViewController encapsulates video, then should add AVPlayerViewController as child:

import UIKit
import AVKit

class IntroductionViewController: UIViewController {

    var avPlayer: AVPlayer!

    override func viewDidLoad() {
        super.viewDidLoad()
        playVideo()
    }

    func playVideo() {

        let filepath: String? = Bundle.main.path(forResource: "videoplayback", ofType: "mp4")
        let fileURL = URL.init(fileURLWithPath: filepath!)

        avPlayer = AVPlayer(url: fileURL)
        let avPlayerController = AVPlayerViewController()
        avPlayerController.player = avPlayer
        avPlayerController.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)

        // Turn on video controlls
        avPlayerController.showsPlaybackControls = true

        // play video
        avPlayerController.player?.play()
        self.view.addSubview(avPlayerController.view)
        self.addChild(avPlayerController)
    }
}

But your IntroductionViewController can extends AVPlayerViewController as well:

import UIKit
import AVKit

class AVViewController: AVPlayerViewController {

    var avPlayer: AVPlayer!

    override func viewDidLoad() {
        super.viewDidLoad()

        setupAndPlay()
    }

    func setupAndPlay() {
        let filepath: String? = Bundle.main.path(forResource: "videoplayback", ofType: "mp4")
        let fileURL = URL.init(fileURLWithPath: filepath!)

        // Setup AVPlayer
        avPlayer = AVPlayer(url: fileURL)
        self.player = avPlayer
        self.view.frame = CGRect(x: 44, y: 128, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)

        // Turn on video controlls
        self.showsPlaybackControls = true

        // play video
        self.player?.play()
    }
}

Choose your way :)

ciccioska
  • 1,291
  • 16
  • 22
  • You shouldn't subclass AVPlayerViewController though. "Do not subclass AVPlayerViewController. Overriding this class’s methods is unsupported and results in undefined behavior." according to doc: https://developer.apple.com/documentation/avkit/avplayerviewcontroller – yuji Jul 08 '19 at 04:16