import UIKit
import VideoEditorSDK
class AddAudioOverlaysFromRemoteURLSwift: Example, VideoEditViewControllerDelegate {
private var downloadLocations = [String: URL]()
override func invokeExample() {
let video = Video(url: Bundle.main.url(forResource: "Skater", withExtension: "mp4")!)
let elsewhereAudioClipIdentifiers = [
"elsewhere",
"trapped_in_the_upside_down"
]
let otherAudioClipIdentifiers = [
"dance_harder",
"far_from_home"
]
let audioClipIdentifiers = elsewhereAudioClipIdentifiers + otherAudioClipIdentifiers
audioClipIdentifiers.forEach { identifier in
guard let remoteURL = URL(string: "https://img.ly/static/example-assets/\(identifier).mp3") else {
fatalError("Unable to parse URL.")
}
let downloadTask = URLSession.shared.downloadTask(with: remoteURL) { downloadURL, _, error in
if let error = error {
fatalError("There was an error downloading the file: \(error)")
}
guard let downloadURL = downloadURL else {
return
}
let temporaryDirectoryURL = FileManager.default.temporaryDirectory
let localURL = temporaryDirectoryURL.appendingPathComponent("\(identifier).mp3")
if FileManager.default.fileExists(atPath: localURL.path) {
try? FileManager.default.removeItem(at: localURL)
}
try? FileManager.default.moveItem(at: downloadURL, to: localURL)
DispatchQueue.main.async {
self.downloadLocations[identifier] = localURL
if self.downloadLocations.count == audioClipIdentifiers.count {
let elsewhereClipsLocations = self.downloadLocations.filter { elsewhereAudioClipIdentifiers.contains($0.key) }
let otherClipsLocations = self.downloadLocations.filter { otherAudioClipIdentifiers.contains($0.key) }
let elsewhereClips = elsewhereClipsLocations.map { AudioClip(identifier: $0.key, audioURL: $0.value) }
let otherClips = otherClipsLocations.map { AudioClip(identifier: $0.key, audioURL: $0.value) }
let elsewhereAudioClipCategory = AudioClipCategory(title: "Elsewhere", imageURL: nil, audioClips: elsewhereClips)
let otherAudioClipCategory = AudioClipCategory(title: "Other", imageURL: nil, audioClips: otherClips)
let configuration = Configuration { builder in
builder.assetCatalog.audioClips = [elsewhereAudioClipCategory, otherAudioClipCategory]
}
let videoEditViewController = VideoEditViewController(videoAsset: video, configuration: configuration)
videoEditViewController.delegate = self
self.presentingViewController?.view.isUserInteractionEnabled = true
videoEditViewController.modalPresentationStyle = .fullScreen
self.presentingViewController?.present(videoEditViewController, animated: true, completion: nil)
}
}
}
self.presentingViewController?.view.isUserInteractionEnabled = false
downloadTask.resume()
}
}
private func removeResources() {
for location in downloadLocations.values {
try? FileManager.default.removeItem(at: location)
}
downloadLocations.removeAll()
}
func videoEditViewControllerShouldStart(_ videoEditViewController: VideoEditViewController, task: VideoEditorTask) -> Bool {
true
}
func videoEditViewControllerDidFinish(_ videoEditViewController: VideoEditViewController, result: VideoEditorResult) {
presentingViewController?.dismiss(animated: true, completion: {
self.removeResources()
})
}
func videoEditViewControllerDidFail(_ videoEditViewController: VideoEditViewController, error: VideoEditorError) {
print(error.localizedDescription)
presentingViewController?.dismiss(animated: true, completion: {
self.removeResources()
})
}
func videoEditViewControllerDidCancel(_ videoEditViewController: VideoEditViewController) {
presentingViewController?.dismiss(animated: true, completion: {
self.removeResources()
})
}
}