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

相关内容

热门资讯

透视科技!hhpoker德州透... 透视科技!hhpoker德州透视挂(透视)都是是真的辅助辅助器(了解有挂)-哔哩哔哩1、每一步都需要...
今天下午!菠萝辅助器免费版的功... 今天下午!菠萝辅助器免费版的功能介绍(透视)好像真的有辅助修改器(有挂方法)-哔哩哔哩在进入菠萝辅助...
透视辅助!wepoker有辅助... 透视辅助!wepoker有辅助吗(透视)一直真的是有辅助神器(果真有挂)-哔哩哔哩1、下载好wepo...
为了进一步!hhpoker有后... 为了进一步!hhpoker有后台操控吗(透视)原来有辅助下载(有挂规律)-哔哩哔哩1、超多福利:超高...
更值得关注的是!aapoker... 更值得关注的是!aapoker脚本怎么用(透视)果然是真的辅助安装(今日头条)-哔哩哔哩1、金币登录...
透视最新!wepoker游戏的... 透视最新!wepoker游戏的安装教程(透视)其实真的有辅助安装(有挂规律)-哔哩哔哩一、wepok...
透视脚本!wepoker作必弊... 透视脚本!wepoker作必弊方法(透视)好像是真的辅助辅助器(有挂教程)-哔哩哔哩1、起透看视 w...
透视辅助!werplan透视挂... 透视辅助!werplan透视挂(透视)其实真的是有辅助修改器(有挂分享)-哔哩哔哩1、游戏颠覆性的策...
反观!wepoker破解工具(... 反观!wepoker破解工具(透视)总是是有辅助app(真的有挂)-哔哩哔哩小薇(辅助器软件下载)致...
今日!wepoker好友房开挂... 今日!wepoker好友房开挂(透视)都是真的有辅助修改器(证实有挂)-哔哩哔哩;wepoker好友...