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

相关内容

热门资讯

解谜透视!拱趴大菠萝万能挂图解... 解谜透视!拱趴大菠萝万能挂图解,微乐小程序辅助器代理,教材教程(有挂解惑)-哔哩哔哩1、玩家可以在拱...
辅助透视!wepoker透视脚... 辅助透视!wepoker透视脚本下载,微乐山西小程序破解器,技法教程(详细教程)-哔哩哔哩1、玩家可...
详细透视!wepoker辅助器... 您好,wepoker辅助器是真的吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【4852750...
必备透视!wepoker有辅助... 必备透视!wepoker有辅助工具吗,微乐自建房脚本免费下载入口,资料教程(有挂讲解)-哔哩哔哩1、...
辅助透视!aapoker怎么拿... 辅助透视!aapoker怎么拿好牌,微乐房间辅助工具在哪下载,要领教程(今日头条)-哔哩哔哩1、每一...
分享透视!德州圈脚本,微乐四川... 分享透视!德州圈脚本,微乐四川亲友圈辅助器,烘培教程(有挂秘诀)-哔哩哔哩1、德州圈脚本辅助器安装包...
解密透视!wpk透视插件,微乐... 解密透视!wpk透视插件,微乐广西小程序脚本,技法教程(有挂总结)-哔哩哔哩wpk透视插件透视方法中...
详情透视!pokemmo辅助工... 详情透视!pokemmo辅助工具,微信小程序微乐辅助器苹果,方式教程(真实有挂)-哔哩哔哩;1、po...
解迷透视!aapoker安装包... 解迷透视!aapoker安装包怎么使用,微信小程序微乐房间怎么辅助,妙计教程(真的有挂)-哔哩哔哩1...
推荐透视!wepoker怎么下... 推荐透视!wepoker怎么下载游戏,微信微乐辅助脚本平台,课程教程(有挂详情)-哔哩哔哩1、游戏颠...