I have this app in which the main purpose is to record video using AVFoundation, and then saving the outputfile delegate.
On first any normal run the app could properly record and save the video, and I could load the resulting outputfile URL into Avplayer I have on the next viewcontroller.
The AVPlayer would play just fine normally if the Xcode is not re-run. What I mean with rerunning is tapping into the Command+R again or clicking on the Play button on xcode to rebuild re-run.
What happens when I run the app again is that I could still see the files and list them via print, but when I'm trying to access the recorded file the app would no longer play the video. It would seem that the video wasn't loaded properly or anything even if it had already been working earlier. After this happens I could still go on record new ones and play the newly recorded ones, but not the old files.
The old files before rebuild of app would just look like this on AVPlayer
Also the AVPlayer would successfully return .readyToPlay status on my observer.
I'm confused how should I go on fixing this one.
if let videoPreviewController = storyBoard.instantiateViewController(withIdentifier: "VideoPreviewController") as? VideoPreviewController {
let url = NSURL(fileURLWithPath: "file:///var/mobile/Containers/Data/Application/61E182ED-D490-4C7D-BAB7-C90D095C7E43/Documents/Thursday,%208%20June%202017%20at%201:47:51%20PM%20Philippine%20Standard%20Time.mov")
videoPreviewController.player = AVPlayer()
let playerItem = AVPlayerItem.init(url: url as URL)
videoPreviewController.player?.replaceCurrentItem(with: playerItem)
videoPreviewController.report = relationship
videoPreviewController.appointment = self.appointment
navigation.pushViewController(videoPreviewController, animated: true)
}
Your problem is this line:
let url = NSURL(fileURLWithPath: "file:///var/mobile/Containers/Data/Application/61E182ED-D490-4C7D-BAB7-C90D095C7E43/Documents/Thursday,%208%20June%202017%20at%201:47:51%20PM%20Philippine%20Standard%20Time.mov")
That path will change every time your restart your app -- you can't hard code it. You have to ask the OS for the path to your documents folder and append your filename to it.
let userDocumentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileName = "Thursday,%208%20June%202017%20at%201:47:51%20PM%20Philippine%20Standard%20Time.mov"
let movieURL = userDocumentsURL.appendingPathComponent(fileName)
And by the way, that's probably how you saved the file in the first place... get the path, append your filename and save it.