When a user records a video from my app, it stores them in a temp file and then puts the URL inside of an array. I just added the UIEditorViewController in my app to allow users to trim there videos.
However, When they save the video after trimming, it doesn't replace the current video in the array, so when they go to watch the video it plays the original video.
Here is my code below:
NOTE: videosArray is an array of the video URL'S they've recorded.
This is the code to enter the video editor view controller
@IBAction func editVideo(_ sender: Any) {
removeInputOutputs() // Function to remove current capture session inputs
videoPlayer?.player?.replaceCurrentItem(with: nil)
// This is the video that they will edit
let video = videosArray[selectedPlayingVideo] // videosArray is a list of URL instances
if UIVideoEditorController.canEditVideo(atPath: video.path) {
let editController = UIVideoEditorController()
editController.videoPath = video.path
editController.delegate = self as? UINavigationControllerDelegate & UIVideoEditorControllerDelegate
editController.toolbar.tintColor = UIColor.white
present(editController, animated:true)
}
}
This is the function for when they save the trimmed video
In the function you will see a variable called replacedVideoPosition. This is a variable that holds the integer value of the position in the array of URLS.
func videoEditorController(_ editor: UIVideoEditorController,
didSaveEditedVideoToPath editedVideoPath: String) {
videoPlayer?.player?.replaceCurrentItem(with: nil)
editorViewLayout() // Go back to original view
videosArray.remove(at: MyVariables.replacedVideoPosition!)
let replacedUrl = URL(fileURLWithPath: editedVideoPath)
videosArray.insert(replacedUrl, at: MyVariables.replacedVideoPosition!)
dismiss(animated:true)
}