Код: Выделить всё
/// Asynchronously merges an array of audio files into a single m4a file using the new async export API (iOS 16+).
/// - Parameters:
/// - fileURLs: The URLs of the audio files to merge, in the order they should be concatenated.
/// - outputURL: The URL for the final merged audio file.
/// - Throws: An error if the merge or export fails.
private nonisolated static func mergeAudioFiles(fileURLs: [URL], outputURL: URL) async throws {
precondition(!fileURLs.isEmpty)
let composition = AVMutableComposition()
guard let compositionTrack = composition.addMutableTrack(
withMediaType: .audio,
preferredTrackID: kCMPersistentTrackID_Invalid
) else {
throw NSError(domain: "MergeError", code: -1, userInfo: [NSLocalizedDescriptionKey: "Could not create composition track"])
}
var currentTime = CMTime.zero
var insertedAny = false
for fileURL in fileURLs {
let asset = AVAsset(url: fileURL)
do {
let _ = try await asset.load(.duration)
let tracks = try await asset.load(.tracks)
guard let assetTrack = tracks.first(where: { $0.mediaType == .audio }) else {
print("Warning: No audio track in \(fileURL.lastPathComponent)")
continue
}
let timeRange = CMTimeRange(start: .zero, duration: asset.duration)
try compositionTrack.insertTimeRange(timeRange, of: assetTrack, at: currentTime)
currentTime = CMTimeAdd(currentTime, asset.duration)
insertedAny = true
} catch {
print("Error processing \(fileURL.lastPathComponent): \(error.localizedDescription)")
}
}
guard insertedAny else {
throw NSError(domain: "MergeError", code: -2, userInfo: [NSLocalizedDescriptionKey: "No valid audio tracks found to merge."])
}
guard let exportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A) else {
throw NSError(domain: "ExportError", code: -1, userInfo: [NSLocalizedDescriptionKey: "Could not create export session"])
}
try? FileManager.default.removeItem(at: outputURL)
exportSession.outputURL = outputURL
exportSession.outputFileType = .m4a
await exportSession.export()
if let error = exportSession.error {
throw error
}
}
< /code>
Этот процесс слияния успешно работает большую часть времени (возможно, в 99% случаев). Тем не менее, несколько клиентов сообщили, что столкнулись с ошибкой. В частности, ошибка выбрасывается при проверке свойства exportsession.error
Код: Выделить всё
await exportSession.export()
// Error occurs here:
if let error = exportSession.error {
// 'error' is non-nil for these customers
print("Export failed with error: \(error)") // Added print for context
throw error
}
Спасибо.
Подробнее здесь: https://stackoverflow.com/questions/796 ... audio-segm