Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to play a video from bundle directory but I'm getting this error:

video[29054:2968406] [MediaRemote] [AVOutputContext] WARNING: AVF context unavailable for sharedAudioPresentationContex

This is my implementation:

import UIKit import AVKit import AVFoundation   class ViewController: UIViewController {     var playerVC = AVPlayerViewController()     var playerView = AVPlayer()      override func viewDidLoad() {         super.viewDidLoad()         self.playVideo()      }       func playVideo() {         let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last! as NSURL         guard let fileURL  = documentsDirectory.appendingPathComponent("video.mp4") else{ return }         self.playerView = AVPlayer(url:fileURL as URL)         self.playerVC.player = playerView         self.view.addSubview(self.playerVC.view)         self.addChildViewController(self.playerVC)         playerView.play()      } 

Any of you knows what I'm doing wrong?, I'll really appreciate your help.

like image 281
user2924482 Avatar asked Oct 06 '17 19:10

user2924482


2 Answers

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

like image 178
dev Avatar answered Sep 28 '22 20:09

dev


This is my implementation. It worked fine for me.

import UIKit import AVFoundation  class 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 !!

like image 26
fazeel ahamed Avatar answered Sep 28 '22 19:09

fazeel ahamed