如果您使用AVMutableComposition创建视频并遇到间歇性静音问题,则很可能是由于您的AVAssetTrack中的音频配置不正确而引起的。
以下是一个可能的解决方案,可以检查您的音频配置是否正确:
let composition = AVMutableComposition()
let videoTrack = composition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)
let audioTrack = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid)
// Add your video and audio assets to each track
if let audioAssetTrack = audioAsset.tracks(withMediaType: .audio).first {
do {
let audioInputParams = AVMutableAudioMixInputParameters(track: audioTrack)
let audioTimeRange = CMTimeRangeMake(start: .zero, duration: audioAssetTrack.timeRange.duration)
try audioInputParams.setVolume(1.0, at: .zero)
try audioInputParams.setAudioTimePitchAlgorithm(AVAudioTimePitchAlgorithm.timeDomain)
audioInputParams.audioTimePitchAlgorithm = .varispeed
try audioInputParams.setAudioMix(myAudioMixes, at: .zero)
audioInputParams.trackID = audioTrack.trackID
if let audioMix = AVMutableAudioMix() {
audioMix.inputParameters = [audioInputParams]
let exportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)
exportSession?.audioMix = audioMix
exportSession?.timeRange = videoTrack.timeRange
exportSession?.outputFileType = .mov
...
}
} catch {
print("Error setting up audio input params: \(error)")
}
}
这里我们使用AVMutableAudioMixInputParameters配置音频轨道,设置音量、时间和音高算法,并将其应用于AVMutableComposition。请注意,您需要确保设置的音频参数与您的音频资源匹配,以避免静音问题。