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)
}
}
3
Answers
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: