ios - 音频导出失败 iOS Swift

标签 ios swift avassetexportsession

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

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!)

关于ios - 音频导出失败 iOS Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36579464/

相关文章:

ios - opengl es(iphone) 从文件渲染

iphone - 从 C 函数返回 NSString

ios - 无法将UITextField聚焦在嵌套的UIView中

ios - 将 MTAudioProcessingTap 与 AVAssetExportSession 结合使用

ios - iOS 13 上的 AVAssetReferenceRestrictions

ios - 使用 AVAsset/AVCaptureSession 裁剪视频

ios - 如何取消或停止来自客户端的推送通知?

swift - 如何确保 Alamofire 请求运行?

swift - 如何在 Swift 中从 parse.com 数据库中读取 unicode 字符?

swift - 如何控制 SwiftUI 图像的无障碍画外音文本