Configure Audio Overlays
VideoEditor SDK supports several configuration options for the AudioToolController
allowing flexible adaptation to different needs and use cases.
For a detailed explanation on how to configure different editor views, refer to this guide.
audioClips
#
audioClips
#In order to add custom audio clips, we first load them from the bundle and add them in an array to the audioClips
property of the assetCatalog
config option.
For details consult the section on adding audio overlays from the app bundle.
allowedAudioOverlayActions
#
allowedAudioOverlayActions
#Configuring the AudioToolController
allows changing the way users can overlay clips on a video.
The actions users can perform are configured via the allowedAudioOverlayActions
property. In our example, we only enable the options to play and pause the audio clip (.playPause
).
defaultAudioClipCategoryIndex
#
defaultAudioClipCategoryIndex
#Configuring the AudioClipToolController
allows changing the way users can interact with audio clip categories.
The defaultAudioClipCategoryIndex
configures which category will be displayed first when opening the selection tool.
In our example, we want the second category to be displayed by default.
import UIKitimport VideoEditorSDKclass AudioOverlayConfigurationSwift: Example, VideoEditViewControllerDelegate {override func invokeExample() {// Create a `Video` from a URL to a video in the app bundle.let video = Video(url: Bundle.main.url(forResource: "Skater", withExtension: "mp4")!)// Identifiers of audio clips in the app bundle.let elsewhereAudioClipIdentifiers = ["elsewhere","trapped_in_the_upside_down"]let otherAudioClipIdentifiers = ["dance_harder","far_from_home"]// Create new audio clip categories with custom audio clips from// the app bundle.let elsewhereClips = elsewhereAudioClipIdentifiers.map { AudioClip(identifier: $0, audioURL: Bundle.main.url(forResource: $0, withExtension: ".mp3")!) }let otherClips = otherAudioClipIdentifiers.map { AudioClip(identifier: $0, audioURL: Bundle.main.url(forResource: $0, withExtension: ".mp3")!) }let elsewhereAudioClipCategory = AudioClipCategory(title: "Elsewhere", imageURL: nil, audioClips: elsewhereClips)let otherAudioClipCategory = AudioClipCategory(title: "Other", imageURL: nil, audioClips: otherClips)// Create a `Configuration` object.let configuration = Configuration { builder in// In this example we are using the default assets for the asset catalog as a base.// However, you can also create an empty catalog as well which only contains your// custom assets.let assetCatalog = AssetCatalog.defaultItems// Add the custom audio clips to the asset catalog.assetCatalog.audioClips.append(contentsOf: [elsewhereAudioClipCategory, otherAudioClipCategory])// Use the new asset catalog for the configuration.builder.assetCatalog = assetCatalog// Configure the `AudioToolController` that lets the user// overlay audio tracks on top of the video.builder.configureAudioToolController { options in// By default the editor allows all available overlay actions// that can be used in this tool. For this example, only the// play/pause button is enabled.options.allowedAudioOverlayActions = [.playPause]}// Configure the `AudioClipToolController` that lets the user// select audio tracks to overlay on top of the video.// highlight-categoriesbuilder.configureAudioClipToolController { options in// By default the editor selects the first audio clip category// when opening the selection tool. For this example the editor// will select the second category.options.defaultAudioClipCategoryIndex = 1}// highlight-categories}// Create and present the video editor. Make this class the delegate of it to handle export and cancelation.let videoEditViewController = VideoEditViewController(videoAsset: video, configuration: configuration)videoEditViewController.delegate = selfvideoEditViewController.modalPresentationStyle = .fullScreenpresentingViewController?.present(videoEditViewController, animated: true, completion: nil)}// MARK: - VideoEditViewControllerDelegatefunc videoEditViewControllerShouldStart(_ videoEditViewController: VideoEditViewController, task: VideoEditorTask) -> Bool {// Implementing this method is optional. You can perform additional validation and interrupt the process by returning `false`.true}func videoEditViewControllerDidFinish(_ videoEditViewController: VideoEditViewController, result: VideoEditorResult) {// The user exported a new video successfully and the newly generated video is located at `result.output.url`. Dismissing the editor.presentingViewController?.dismiss(animated: true, completion: nil)}func videoEditViewControllerDidFail(_ videoEditViewController: VideoEditViewController, error: VideoEditorError) {// There was an error generating the video.print(error.localizedDescription)// Dismissing the editor.presentingViewController?.dismiss(animated: true, completion: nil)}func videoEditViewControllerDidCancel(_ videoEditViewController: VideoEditViewController) {// The user tapped on the cancel button within the editor. Dismissing the editor.presentingViewController?.dismiss(animated: true, completion: nil)}}