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秒开始播放,第二个音频文件从第一个音频文件

相关内容

热门资讯

解谜透视!aapoker透视方... 解谜透视!aapoker透视方法,微信小程序微乐辅助器破解版,教材教程(竟然有挂)-哔哩哔哩1.aa...
揭幕透视!hhpoker免费辅... 揭幕透视!hhpoker免费辅助器,微乐山西威信小程序,绝活儿教程(有挂秘籍)-哔哩哔哩hhpoke...
揭露透视!wepoker有没有... 揭露透视!wepoker有没有挂,万能透视辅助器免费版,诀窍教程(有挂详细)-哔哩哔哩1、wepok...
解谜透视!wepoker可以开... 您好,wepoker可以开透视吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670430...
总结透视!wepoker私人局... 总结透视!wepoker私人局可以透视,微信小程序微乐怎么才能发好牌,经验教程(有挂神器)-哔哩哔哩...
总结透视!aapoker辅助插... 总结透视!aapoker辅助插件工具,微信微乐辅助,手筋教程(有挂规律)-哔哩哔哩1、游戏颠覆性的策...
了解透视!sohoo开挂辅助,... 了解透视!sohoo开挂辅助,微乐小程序有脚本,积累教程(有挂方略)-哔哩哔哩1、完成sohoo开挂...
有挂透视!约局吧能不能开挂,微... 有挂透视!约局吧能不能开挂,微信小程序微乐房间怎么开挂,方式教程(有挂解密)-哔哩哔哩1、首先打开约...
有挂透视!聚星ai辅助工具收费... 有挂透视!聚星ai辅助工具收费多少,手机微乐小程序破解器,方案教程(有挂技巧)-哔哩哔哩;1、有挂透...
详情透视!hh poker辅助... 详情透视!hh poker辅助有用吗,微乐小程序免费脚本,妙招教程(有挂讲解)-哔哩哔哩hh pok...