title: Can't play video with AVKit
body: <p>I am trying to make a video player but somehow I can't see the video playing, although I can hear its sound. I've tried using AVPlayerViewController like so:</p>
  <pre><code>import UIKit
  import AVKit
  
  class VideoPlayerViewController: UIViewController {
      
      private var videoPlayer: AVPlayer?
      
      private let videoUrls = [
          &quot;https://wolverine.raywenderlich.com/content/ios/tutorials/video_streaming/foxVillage.mp4&quot;,
          &quot;http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4&quot;,
          &quot;http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4&quot;
      ]
      
      override func viewDidLoad() {
          super.viewDidLoad()
  
          setupVideoPlayer()
      }
      
      override func viewDidAppear(_ animated: Bool) {
          super.viewDidAppear(animated)
          
          presentVideoPlayerViewController()
      }
      
      private func setupVideoPlayer() {
          guard let url = URL(string: videoUrls.randomElement()!) else { return }
          videoPlayer = AVPlayer(url: url)
      }
  
      private func presentVideoPlayerViewController() {
          guard let player = videoPlayer else { return }
          let playerViewController = AVPlayerViewController()
          playerViewController.player = player
  
          present(playerViewController, animated: true) {
            player.play()
          }
      }
  }
  </code></pre>
  <p>And using AVPlayerLayer like so:</p>
  <pre><code>import UIKit
  import AVKit
  
  class VideoPlayerViewController: UIViewController {
  
      @IBOutlet weak var videoPlayerView: UIView!
      
      private var videoPlayer: AVPlayer?
      private var videoPlayerLayer: AVPlayerLayer?
      
      private let videoUrls = [
          &quot;https://wolverine.raywenderlich.com/content/ios/tutorials/video_streaming/foxVillage.mp4&quot;,
          &quot;http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4&quot;,
          &quot;http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4&quot;
      ]
      
      override func viewDidLoad() {
          super.viewDidLoad()
  
          setupVideoPlayer()
      }
  
      override func viewDidAppear(_ animated: Bool) {
          super.viewDidAppear(animated)
  
          setupVideoPlayerView()
          videoPlayer?.play()
       }
      
      private func setupVideoPlayer() {
          guard let url = URL(string: videoUrls.randomElement()!) else { return }
          videoPlayer = AVPlayer(url: url)
      }
  
      private func setupVideoPlayerView() {
          guard let playerLayer = getPlayerLayer(from: videoPlayer) else {
              return
          }
          
          videoPlayerView.layer.addSublayer(playerLayer)
          
          videoPlayerLayer?.removeFromSuperlayer()
          videoPlayerLayer = playerLayer
      }
  
      private func getPlayerLayer(from player: AVPlayer?) -&gt; AVPlayerLayer? {
          guard let player = player else {
              return nil
          }
  
          let playerLayer = AVPlayerLayer(player: player)
          playerLayer.videoGravity = .resizeAspect
          playerLayer.frame = videoPlayerView.bounds
          return playerLayer
      }
  }
  </code></pre>
  <p>Both worked on an iOS 14 simulator. But I've got these errors in an iOS 11.4 simulator:</p>
  <pre><code>2021-08-04 20:56:42.723171+0700 Video player[31906:850466] Task &lt;5845D991-1144-40FE-AD2A-1AA98E73F9D7&gt;.&lt;2&gt; finished with error - code: -999
  2021-08-04 20:56:44.071786+0700 Video player[31906:850466] Task &lt;3B49CB9C-97ED-46AC-BC9C-11B126625311&gt;.&lt;3&gt; finished with error - code: -999
  2021-08-04 20:56:44.382677+0700 Video player[31906:850653] Task &lt;8DEA7E92-4E54-4105-9A5A-801320090CB6&gt;.&lt;1&gt; finished with error - code: -999
  2021-08-04 20:56:47.080039+0700 Video player[31906:850881] [AudioHAL_Client] HALB_IOBufferManager.cpp:226:GetIOBuffer:  HALB_IOBufferManager::GetIOBuffer: the stream index is out of range
  2021-08-04 20:56:47.082504+0700 Video player[31906:850881] [AudioHAL_Client] HALB_IOBufferManager.cpp:226:GetIOBuffer:  HALB_IOBufferManager::GetIOBuffer: the stream index is out of range
  2021-08-04 20:56:47.313237+0700 Video player[31906:850881] [aqme] 254: AQDefaultDevice (1): output stream 0: null buffer
  2021-08-04 20:56:47.315029+0700 Video player[31906:850881] [aqme] 1433: EXCEPTION thrown (-50): -
  2021-08-04 20:57:01.906151+0700 Video player[31906:850653] [aqme] 177: timed out after 15.000s (0 1); suspension count=0 (IOSuspensions: )
  2021-08-04 20:57:01.975129+0700 Video player[31906:850653] 239: CA_UISoundClientBase::StartPlaying: AddRunningClient failed (status = -66681).
  2021-08-04 20:57:02.911820+0700 Video player[31906:851213] [AudioHAL_Client] HALB_IOBufferManager.cpp:226:GetIOBuffer:  HALB_IOBufferManager::GetIOBuffer: the stream index is out of range
  2021-08-04 20:57:02.916048+0700 Video player[31906:851213] [AudioHAL_Client] HALB_IOBufferManager.cpp:226:GetIOBuffer:  HALB_IOBufferManager::GetIOBuffer: the stream index is out of range
  2021-08-04 20:57:02.960249+0700 Video player[31906:851213] [aqme] 254: AQDefaultDevice (1): output stream 0: null buffer
  2021-08-04 20:57:02.970924+0700 Video player[31906:851213] [aqme] 1433: EXCEPTION thrown (-50): -
  2021-08-04 20:57:17.637706+0700 Video player[31906:850656] Task &lt;53514ED1-3A24-4BA2-A676-17A7B8FC7165&gt;.&lt;3&gt; finished with error - code: -999
  2021-08-04 20:57:17.672290+0700 Video player[31906:851196] [aqme] 177: timed out after 15.000s (1 2); suspension count=0 (IOSuspensions: )
  2021-08-04 20:57:28.633691+0700 Video player[31906:851601] Task &lt;CC6ED234-5DCF-49DD-8D9F-4D600E943760&gt;.&lt;4&gt; finished with error - code: -999
  2021-08-04 20:57:28.747661+0700 Video player[31906:851627] [AudioHAL_Client] HALB_IOBufferManager.cpp:226:GetIOBuffer:  HALB_IOBufferManager::GetIOBuffer: the stream index is out of range
  2021-08-04 20:57:28.750375+0700 Video player[31906:851627] [AudioHAL_Client] HALB_IOBufferManager.cpp:226:GetIOBuffer:  HALB_IOBufferManager::GetIOBuffer: the stream index is out of range
  2021-08-04 20:57:28.921880+0700 Video player[31906:851627] [aqme] 254: AQDefaultDevice (1): output stream 0: null buffer
  2021-08-04 20:57:28.932579+0700 Video player[31906:851627] [aqme] 1433: EXCEPTION thrown (-50): -
  2021-08-04 20:57:28.992942+0700 Video player[31906:851601] Task &lt;DF0FCD41-0E01-46EC-85F8-257C798C1284&gt;.&lt;5&gt; finished with error - code: -999
  2021-08-04 20:57:43.128655+0700 Video player[31906:851616] [aqme] 177: timed out after 15.000s (2 3); suspension count=0 (IOSuspensions: )
  </code></pre>
  <p>BTW, I'm using Swift 5 with XCode 12.5.1. My OS is Big Sur 11.4. Can anybody help me fix this? Thanks.</p>