Setting multiple Volumes to each Video tracks using AudioMixInputParameters AVFoundation is not working in Swift iOS

前端 未结 2 523
余生分开走
余生分开走 2020-12-18 13:28

I am working on Video based Application in Swift. As per the requirement I have to select multiple Videos from Devi

2条回答
  •  借酒劲吻你
    2020-12-18 14:07

    I found, that exporting an asset with preset of AVAssetExportPresetPassthrough doesn't have an impact on output volume. When I tried to use AVAssetExportPresetLowQuality, volume change successfully applied.

    I wish it is better documented somewhere :(

    The working code:

    // Assume we have:
    let composition: AVMutableComposition
    var inputParameters = [AVAudioMixInputParameters]()
    
    // We add a track
    let trackComposition = composition.addMutableTrack(...)
    
    // Configure volume for this track
    let inputParameter = AVMutableAudioMixInputParameters(track: trackComposition)
    inputParameter.setVolume(desiredVolume, at: startTime)
    
    // It works even without setting the `trackID`
    // inputParameter.trackID = trackComposition.trackID
    
    inputParameters.append(inputParameter)
    
    // Apply gathered `inputParameters` before exporting
    let audioMix = AVMutableAudioMix()
    audioMix.inputParameters = inputParameters
    
    // I found it's not working, if using `AVAssetExportPresetPassthrough`,
    // so try `AVAssetExportPresetLowQuality` first
    let export = AVAssetExportSession(..., presetName: AVAssetExportPresetLowQuality)
    export.audioMix = audioMix
    

    Tested this with multiple assetTrack insertions to the same compositionTrack, setting different volume for each insertion. Seems to be working.

提交回复
热议问题