What I want is basically reverse an AVAsset and output video file and I found this link -
https://github.com/tempire/ReverseAVAsset/blob/master/AVAsset.swift
This is what I did:
import UIKit
import AVFoundation
import AVKit
class ViewController: UIViewController {
var playerViewController = AVPlayerViewController()
var playerView = AVPlayer()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let path = Bundle.main.path(forResource: "video", ofType: "mov")
let outputUrl = URL(fileURLWithPath: "\(NSUUID().uuidString)video.mov")
let originalAsset = AVAsset(url: URL(fileURLWithPath: path!))
let reversedAsset = originalAsset.reversedAsset(outputUrl)
playerView = AVPlayer(playerItem: AVPlayerItem(asset: reversedAsset!))
playerViewController.player = playerView
present(playerViewController, animated: true) {
self.playerViewController.player?.play()
}
}
}
But its not working and this is what I end up with:
I think I'm doing something wrong with outputURL, please help if you know what to do
Problem was with output URL, this is the correct way to do
import UIKit
import AVFoundation
import AVKit
class ViewController: UIViewController {
var playerViewController = AVPlayerViewController()
var playerView = AVPlayer()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let path = Bundle.main.path(forResource: "video", ofType: "mov")
//Correct way
var outputURL: URL?
do {
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
outputURL = documentsURL.appendingPathComponent("\(NSUUID().uuidString)newvideo.mp4")
}
let originalAsset = AVAsset(url: URL(fileURLWithPath: path!))
let reversedAsset = originalAsset.reversedAsset(outputUrl)
playerView = AVPlayer(playerItem: AVPlayerItem(asset: reversedAsset!))
playerViewController.player = playerView
present(playerViewController, animated: true) {
self.playerViewController.player?.play()
}
}
}
The main issue was there in Method: reversedAsset(outputUrl)
There we try to write the file with a completion handler, and meanwhile we try to play the asset returned by the same method which is not even completely written on disc.
In order to resolve the issue, You've to wait for the method reversedAsset(outputUrl) to finish writing the file.
Related
I have recorded a video from my app which saves the video in Photos app in iPhone. The file path of the video saved is:
file:///var/mobile/Containers/Data/Application/0A8F1D57-7336-4D52-8B64-928C381DBC4F/Documents/2022-04-14#14-26-59GMT+05:00ARVideo.mp4
How can I play the video from the file path in AVPlayer?
class SecondViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
playVideo()
}
private func playVideo() {
guard let filePath = Bundle.main.path(forResource: "file:///var/mobile/Containers/Data/Application/0A8F1D57-7336-4D52-8B64-928C381DBC4F/Documents/2022-04-14#14-26-59GMT+05:00ARVideo.mp4", ofType: "mp4") else { return}
let player = AVPlayer(url: URL(fileURLWithPath: filePath))
let vc = AVPlayerViewController()
vc.player = player
present(vc, animated: true)
}
}
import AVKit
func playVideo(){
let player = AVPlayer(url: URL(video url string) ?? URL(fileURLWithPath: "") )
let playerController = AVPlayerViewController()
playerController.player = player
self.addChild(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
playerController.exitsFullScreenWhenPlaybackEnds = true
player.play()
}
class SecondViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
playVideo()
}
// if u have video in app directory/ document directory
private func playVideo() {
guard let url = URL(string: "file:///var/mobile/Containers/Data/Application/0A8F1D57-7336-4D52-8B64-928C381DBC4F/Documents/2022-04-14#14-26-59GMT+05:00ARVideo.mp4")
else {
print("url not exist")
return
}
let player = AVPlayer(url: url)
let vc = AVPlayerViewController()
vc.player = player
present(vc, animated: true)
}
// if u have video locally that u put in ur project then
private func playVideoFromLocal() {
guard let fileURL = Bundle.main.url(forResource: "file_example_MOV_480_700kB", withExtension: "mov") else {return}
let player = AVPlayer(url: fileURL)
let playerController = AVPlayerViewController()
let vc = AVPlayerViewController()
vc.player = player
present(vc, animated: true)
}
}
The path of the documents changes for the app each time it is launched, which changes the first part of the URL.
Try the following updated function to get the current location of the file:
private func playVideo() {
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let playerItem = AVPlayerItem(url: URL(fileURLWithPath: documentsPath.appendingFormat("/your-video-name-here.mp4")))
let player = AVPlayer(playerItem: playerItem)
let vc = AVPlayerViewController()
vc.player = player
present(vc, animated: true)
}
So I have a view on my HomeViewController that I want to play a video inside of. on viewDidLoad() I am calling playVideo() here is my code:
func playVideo() {
guard let filePath = Bundle.main.path(forResource: "video", ofType: "mov") else {
debugPrint("video.mov not found.")
return
}
let avPlayer = AVPlayer(url: URL(fileURLWithPath: filePath))
let avPlayerController = AVPlayerViewController()
avPlayerController.player = avPlayer
avPlayerController.view.frame = playerView.bounds
avPlayerController.showsPlaybackControls = false
avPlayerController.player?.play()
playerView.addSubview(avPlayerController.view)
playerView.bringSubview(toFront: QRButton)
}
All I get is a black background and no video. What am I doing wrong? I have tried to solve this for a while now.
iOS: 11.2 beta
Xcode: 9.2 beta
Try to use avPlayer as a class variable, maybe it has been released before it can start to play.
class ViewController: UIViewController {
var avPlayer:AVPlayer?
func playVideo() {
guard let filePath = Bundle.main.path(forResource: "video", ofType: "mov") else {
debugPrint("video.mov not found.")
return
}
avPlayer? = AVPlayer(url: URL(fileURLWithPath: filePath))
let avPlayerController = AVPlayerViewController()
avPlayerController.player = avPlayer
avPlayerController.view.frame = playerView.bounds
avPlayerController.showsPlaybackControls = false
avPlayerController.player?.play()
playerView.addSubview(avPlayerController.view)
playerView.bringSubview(toFront: QRButton)
}
}
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.
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 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 !!
import UIKit
import AVKit
import AVFoundation
class 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()
}
}
I have an audio files, stored in Document directory. I want play it using AVPlayer but it is not playing. I am using Swift3.
var myurl : NSURL!
var audioPlayer = AVPlayer()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Getting sound from Document directory
let fileManager = FileManager.default
let docsurl = try! fileManager.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
myurl = docsurl.appendingPathComponent("/\(songName!)")
playSound(url: myurl! as NSURL)
}
// Playing Sound
func playSound(url:NSURL){
let asset = AVURLAsset(url: url as URL, options: nil)
let playerItem = AVPlayerItem(asset: asset)
self.audioPlayer.replaceCurrentItem(with: playerItem)
let durationInSeconds = CMTimeGetSeconds(asset.duration)
self.getSeconds = Int(durationInSeconds)
self.audioPlayer.play()
}
I am getting url from document directory, but it is not playing. Don't know why?
Thanks in Advance.
Put this code in AppDelegate.swift file in application:didFinishLaunchingWithOptions: method and see if you can hear the audio:
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
}catch {
print(error)
}
I received video from node sever in chunks of data. But video does not play in AVPlayer. Here is my code.
let videoUrl = http://staging.teemo.me/api/video/stream/sample12.MOV
playVideo(path:videoUrl, self)
func playVideo(path:String, controller:UIViewController){
let yourFinalVideoURL = path
try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [])
if (yourFinalVideoURL != "") {
let player = AVPlayer(url: NSURL(fileURLWithPath: yourFinalVideoURL) as URL)
let playerController = AVPlayerViewController()
playerController.player = player
controller.present(playerController, animated: true) {
//player.play()
if #available(iOS 10.0, *) {
player.playImmediately(atRate: 1.0)
} else {
player.play()
}
}
}
}
The follow code works for me:
let videoURL = URL(string: "Some url")
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
However when I tried to use your url it doesn't work.
I added the NSAppTransportSecurity in .plist, but it didn't work either.
It is obvious that you have a problem with the url, try to change the extension of the file sample12.MOV to sample12.mp4