Swift: AVPlayer playing video is showing this error: [AVOutputContext] WARNING: AVF context unavailable for sharedAudioPresentationContext Swift: AVPlayer playing video is showing this error: [AVOutputContext] WARNING: AVF context unavailable for sharedAudioPresentationContext ios ios

Swift: AVPlayer playing video is showing this error: [AVOutputContext] WARNING: AVF context unavailable for sharedAudioPresentationContext


I had similar issue with mp4 file stored on device which got fixed by prepending "file://" to the file path

 guard let strPath = Bundle.main.path(forResource: "demo", ofType: "mp4"), let url = URL(string: "file://\(strPath)") else {        print("Umm, looks like an invalid URL!")        return    }

Inspired by this post


This is my implementation. It worked fine for me.

import UIKitimport AVFoundationclass ViewController: UIViewController {    var playerViewController = AVPlayerViewController()    var player = AVPlayer()override func viewDidLoad() {    super.viewDidLoad()    self.playVideo()}func playVideo () {   //creating your document folder url    if let documentsDirectoryURL =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {        // creating your destination folder url        let destinationUrl = documentsDirectoryURL.appendingPathComponent("video.mp4")        self.player = AVPlayer(url: destinationUrl)        self.playerViewController.player = self.player        self.playerViewController.view.frame = self.view.frame        self.view.addSubview(self.playerViewController.view)        self.playerViewController.player?.play()    }}}

Hope this helps you !!


import UIKitimport AVKitimport AVFoundationclass ViewController: UIViewController {    var playerVC = AVPlayerViewController()    var playerView = AVPlayer()    override func viewDidLoad() {        super.viewDidLoad()    }override func viewDidAppear(_ animated: Bool) {       let fileURL = NSURL(fileURLWithPath: "video.mp4")        playerView = AVPlayer(url: fileURL as URL)        playerViewController.player = playerView        self.present(playerViewController, animated: true) {        self.playerViewController.player?.play()        }    }