I’m merely attempting to merge an array of audio recordsdata. Each stack overflow thread for that is outdated and would not work. I get the warning “Think about using asynchronous different perform” on exportSession?.exportAsynchronously {. Im unsure why this occurs. I would respect the assistance.
func mergeAudios(recordings: [Recording], completion: @escaping (_ exporter: AVAssetExportSession) -> ()) async {
let composition = AVMutableComposition()
var lastTime: CMTime = .zero
guard let audioTrack = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid) else { return }
for recording in recordings {
let asset = AVURLAsset(url: recording.fileURL)
do {
strive await audioTrack.insertTimeRange(CMTimeRange(begin: .zero, period: asset.load(.period)), of: asset.loadTracks(withMediaType: .audio)[0], at: lastTime)
} catch {
print(error.localizedDescription)
}
do {
lastTime = strive await CMTimeAdd(lastTime, asset.load(.period))
} catch {
print(error.localizedDescription)
}
}
// MARK: Temp Output URL
let tempURL = URL(fileURLWithPath: NSTemporaryDirectory() + "MergedAudio-(Date()).mp3")
let exportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)
exportSession?.outputFileType = .m4a
exportSession?.outputURL = tempURL
exportSession?.exportAsynchronously {
completion(exportSession!)
}
}
public struct Recording: Hashable {
public let uid: UUID
public var fileURL: URL
public init(fileURL: URL) {
uid = UUID()
self.fileURL = fileURL
}
}