AVAssetExportSession正在导出带有错误时间范围的音频
创始人
2024-11-12 20:00:40
0

通常这个错误是由于导出的音频文件与原始音频文件有时间范围的重叠或不连续所造成的。要解决这个问题,需要在导出音频之前对原始音频进行修正或合并。以下是一个示例代码,演示如何使用AVMutableComposition和AVAssetExportSession合并两个音频文件:

let composition = AVMutableComposition()
let compositionTrack = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid)

let asset1 = AVURLAsset(url: URL(string: "path/to/first/audio/file")!)
let track1 = asset1.tracks(withMediaType: .audio)[0]
let timeRange1 = CMTimeRangeMake(start: CMTime.zero, duration: asset1.duration)

let asset2 = AVURLAsset(url: URL(string: "path/to/second/audio/file")!)
let track2 = asset2.tracks(withMediaType: .audio)[0]
let timeRange2 = CMTimeRangeMake(start: CMTime.zero, duration: asset2.duration)

do {
    try compositionTrack?.insertTimeRange(timeRange1, of: track1, at: CMTime.zero)
    try compositionTrack?.insertTimeRange(timeRange2, of: track2, at: asset1.duration)
} catch {
    print("Failed to insert audio tracks into composition")
}

let exportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)
exportSession?.outputFileType = .m4a
exportSession?.outputURL = URL(string: "path/to/output/file")!

exportSession?.exportAsynchronously {
    if exportSession?.status == .completed {
        print("Audio export successful!")
    } else {
        print("Audio export failed: \(exportSession?.error?.localizedDescription ?? "")")
    }
}

在这个示例中,我们创建了一个AVMutableComposition,并将两个音频文件的音轨插入到组合中。第一个音频文件从0秒开始播放,第二个音频文件从第一个音频文件

相关内容

热门资讯

三分钟妙计!wepoker私人... 三分钟妙计!wepoker私人局有透视吗(透视)一直真的有辅助工具(哔哩哔哩)1、三分钟妙计!wep...
七分钟方针!拱趴大菠萝自动计算... 七分钟方针!拱趴大菠萝自动计算机器人(透视)原来存在有辅助辅助(哔哩哔哩)1、完成拱趴大菠萝自动计算...
第7分钟大纲!we-poker... 第7分钟大纲!we-poker软件(透视)竟然真的有辅助辅助(哔哩哔哩)该软件可以轻松地帮助玩家将w...
1分钟大纲!破解辅助插件wep... 1分钟大纲!破解辅助插件wepoker(透视)真是存在有辅助教程(哔哩哔哩)1、起透看视 破解辅助插...
两分钟要领!hhpoker德州... 两分钟要领!hhpoker德州有挂吗(透视)一直有辅助开挂(哔哩哔哩)1、玩家可以在hhpoker德...
一分钟讲义!拱趴大菠萝有什么挂... 一分钟讲义!拱趴大菠萝有什么挂(透视)竟然是有辅助安装(哔哩哔哩)1、每一步都需要思考,不同水平的挑...
第七分钟手筋!wepoker永... 第七分钟手筋!wepoker永久免费脚本(透视)一直是有辅助透视(哔哩哔哩)1、第七分钟手筋!wep...
第七分钟课程!wepoker软... 第七分钟课程!wepoker软件辅助程序(透视)一贯是真的有辅助安装(哔哩哔哩)wepoker软件辅...
2分钟指引!拱趴大菠萝自动计算... 2分钟指引!拱趴大菠萝自动计算机器人(透视)原来真的有辅助透视(哔哩哔哩)1、拱趴大菠萝自动计算机器...
第六分钟窍要!wpk真吗(透视... 第六分钟窍要!wpk真吗(透视)真是是真的有辅助插件(哔哩哔哩)1、wpk真吗免费辅助多个强度级别选...