音频导出失败 iOS Swift

Audio export fail iOS Swift

我正在尝试使用以下代码连接两个音频文件:

func concatenateFiles(audioFiles: [NSURL], completion: (concatenatedFile: NSURL?) -> ()) {
    // Result file
    var nextClipStartTime = kCMTimeZero
    let composition = AVMutableComposition()
    let track = composition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid)

    // Add each track
    for audio in audioFiles {
        let asset = AVURLAsset(URL: NSURL(fileURLWithPath: audio.path!), options: nil)
        if let assetTrack = asset.tracksWithMediaType(AVMediaTypeAudio).first {
            let timeRange = CMTimeRange(start: kCMTimeZero, duration: asset.duration)
            do {
                try track.insertTimeRange(timeRange, ofTrack: assetTrack, atTime: nextClipStartTime)
                nextClipStartTime = CMTimeAdd(nextClipStartTime, timeRange.duration)
            } catch {
                print("Error concatenating file - \(error)")
                completion(concatenatedFile: nil)
                return
            }
        }
    }

    // Export the new file
    if let exportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetPassthrough) {
        let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
        let documents = NSURL(string: paths.first!)

        if let fileURL = documents?.URLByAppendingPathComponent("\(stringFromDate(NSDate())).m4a") {
            // Remove existing file
            do {
                try NSFileManager.defaultManager().removeItemAtPath(fileURL.path!)
                print("Removed \(fileURL)")
            } catch {
                print("Could not remove file - \(error)")
            }

            // Configure export session output
            exportSession.outputURL = fileURL
            exportSession.outputFileType = AVFileTypeAppleM4A
            // Perform the export
            exportSession.exportAsynchronouslyWithCompletionHandler() { handler -> Void in
                if exportSession.status == .Completed {
                    print("Export complete")
                    dispatch_async(dispatch_get_main_queue(), {
                        completion(concatenatedFile: fileURL)
                    })
                    return
                } else if exportSession.status == .Failed {
                    print("Export failed - \(exportSession.error)")
                }

                completion(concatenatedFile: nil)
                return
            }
        }
    }
}

但我在导出文件时收到此错误:

Export failed - Optional(Error Domain=AVFoundationErrorDomain Code=-11838 "Operation Stopped" UserInfo={NSLocalizedDescription=Operation Stopped, NSLocalizedFailureReason=The operation is not supported for this media.})

我试过更改格式但没有用,我不知道, 有人可以帮助我吗?

我真的不知道为什么,但是当我在exportSession中更改outputURL的属性时,问题就解决了,

之前:

exportSession.outputURL = fileURL

现在:

exportSession.outputURL = NSURL.fileURLWithPath(fileURL.path!)