Попробуйте это:
func mergeAudioFiles(files: [URL], completion: @escaping (_ succeeded: Bool)->()) {
let composition = AVMutableComposition()
guard let compositionAudioTrack:AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: kCMPersistentTrackID_Invalid) else {
completion(false)
return
}
for fileUrl in files {
let asset = AVURLAsset(url: fileUrl, options: nil)
let assetTrack = asset.tracks(withMediaType: AVMediaType.audio)[0]
do {
try compositionAudioTrack.insertTimeRange(assetTrack.timeRange, of: assetTrack, at: compositionAudioTrack.timeRange.duration)
} catch {}
}
guard let exporter = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A) else {
completion(false)
return
}
exporter.outputFileType = AVFileType.m4a
exporter.outputURL = URL(string: "AudioPathInDocumentsDirectory")
exporter.timeRange = compositionAudioTrack.timeRange
exporter.exportAsynchronously() {
switch exporter.status {
case .unknown,.waiting, .exporting:
print("Exported Waiting")
case .completed:
print("Exported Complete")
completion(true)
case .failed:
print("Exported Failed")
completion(false)
case .cancelled:
print("Exported Cancelled")
}
}
}