我是swift和编程方面的新手,我尝试连接一些记录器文件,并成功完成了这些工作:
func concatenateFiles(audioFiles: [URL], completion: @escaping (_ concatenatedFile: NSURL?) -> ()) {
// Result file
var nextClipStartTime = kCMTimeZero
let composition = AVMutableComposition()
let track = composition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid)
// Add each track
for audio in audioFiles {
let asset = AVURLAsset(url: NSURL(fileURLWithPath: audio.path) as URL, options: nil)
if let assetTrack = asset.tracks(withMediaType: AVMediaTypeAudio).first {
let timeRange = CMTimeRange(start: kCMTimeZero, duration: asset.duration)
do {
try track.insertTimeRange(timeRange, of: assetTrack, at: nextClipStartTime)
nextClipStartTime = CMTimeAdd(nextClipStartTime, timeRange.duration)
} catch {
print("Error concatenating file - \(error)")
completion(nil)
return
}
}
}
// Export the new file
if let exportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A) {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let format = DateFormatter()
format.dateFormat = "yyyy:MM:dd-HH:mm:ss"
let currentFileName = "REC:\(format.string(from: Date()))"
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentsDirectory.appendingPathComponent("\(currentFileName).m4a")
// Remove existing file
do {
print(audioFiles.count)
try FileManager.default.removeItem(atPath: fileURL.path)
print("Removed \(fileURL)")
} catch {
print("Could not remove file - \(error)")
}
// Configure export session output
exportSession.outputURL = fileURL as URL
exportSession.outputFileType = AVFileTypeAppleM4A
// Perform the export
exportSession.exportAsynchronously() { () -> Void in
switch exportSession.status
{
case AVAssetExportSessionStatus.completed:
print("Export complete")
DispatchQueue.main.async(execute: {
if self.concatinatedArray == nil
{
self.concatinatedArray = [URL]()
}
self.concatinatedArray?.append(exportSession.outputURL!)
completion(fileURL as NSURL?)
})
return print("success to Merge Video")
case AVAssetExportSessionStatus.failed:
completion(nil)
return print("failed to MERGE )")
case AVAssetExportSessionStatus.cancelled:
completion(nil)
return print("cancelled merge)")
default:
print("complete")
}
}
}
}
但是现在,当我想将其与视频合并时,我突然崩溃了:
let aAudioAssetTrack: AVAssetTrack = aAudioAsset.tracks(withMediaType: AVMediaTypeAudio)[0]
我使用标准的合并方法,它可以与我拥有的其他声音一起使用,不仅可以与级联的音频文件一起使用,请帮助管理它的工作。...
最佳答案
AVURLAsset* avAsset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:path2] options:nil];
if ([[avAsset tracksWithMediaType:AVMediaTypeAudio] count] > 0)
{
AVAssetTrack *clipAudioTrack = [[avAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
[firstTrackAudio insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset.duration) ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil];
}
关于ios - AVAssetTrack没有看到AVMediaTypeAudio,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41762256/