Change volume level of audio asset and merge with another audio/video asset

I have tried using AVMutableAudioMix to change volume level of an audio asset and the audio component of a video file. The composition has both audio assets merged with the video but the volume levels do not change.

let aVideoAssetTrack: AVAssetTrack = aVideoAsset.tracks(withMediaType: AVMediaType.video)[0]
let aAudioOfVideoAssetTrack: AVAssetTrack = aVideoAsset.tracks(withMediaType: AVMediaType.audio).first!
let aAudioAssetTrack: AVAssetTrack = aAudioAsset.tracks(withMediaType: AVMediaType.audio)[0]

    let volumeForOriginalAudio = AVMutableAudioMixInputParameters(track: aAudioOfVideoAssetTrack)
    volumeForOriginalAudio.trackID = aAudioOfVideoAssetTrack.trackID
    volumeForOriginalAudio.setVolume(originalAudioVolume, at: CMTime.zero) // CMTimeMakeWithSeconds(0, preferredTimescale: 1)
    let volumeForAddedTrack = AVMutableAudioMixInputParameters(track: aAudioAssetTrack)
    volumeForAddedTrack.trackID = aAudioAssetTrack.trackID
    volumeForAddedTrack.setVolume(addedAudioVolume, at: CMTime.zero)
    let audioMix = AVMutableAudioMix()
    let audioInputParameters:[AVAudioMixInputParameters] = [volumeForOriginalAudio,volumeForAddedTrack]
    audioMix.inputParameters = audioInputParameters

and then

    let assetExport = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)!
    assetExport.outputFileType = AVFileType.mp4
    assetExport.outputURL = savePathUrl
    assetExport.shouldOptimizeForNetworkUse = true
    assetExport.audioMix = audioMix

This topic was automatically closed after 166 days. New replies are no longer allowed.