iosvideoswift5avassetexportsessionaudio-video-sync

In Production Mode - I got "Operation Stopped" error when exporting video


Please help me to solve this issue when app goes on production mode i got "Operation stopped" error every time when i make video

=> code for adding audio to track

let audioMix = AVMutableAudioMix()
            
let musicAudioTrack: AVMutableCompositionTrack = mixComposition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: Int32(kCMPersistentTrackID_Invalid))!
            
            do {
                
                try musicAudioTrack.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: runningTime!), of: musicAsset.tracks(withMediaType: AVMediaType.audio)[0], at: CMTime.zero)
                musicAudioTrack.preferredVolume = self.volumeSlider.value               
                let audioMixInputParams = AVMutableAudioMixInputParameters()
                audioMixInputParams.trackID = musicAudioTrack.trackID
                audioMixInputParams.setVolume(self.volumeSlider.value, at: CMTime.zero)
                audioMix.inputParameters.append(audioMixInputParams)
               
            } catch {
                
            }

=> Code for MainInstruction

let mainInstruction = AVMutableVideoCompositionInstruction()
mainInstruction.timeRange = CMTimeRangeMake(start: CMTime.zero, duration: runningTime!) 
mainInstruction.layerInstructions = [videoLayerInstructionFront!,videoLayerIntruction1!,videoLayerIntruction2,videoLayerIntruction3,videoLayerIntruction4,videoLayerInstructionEnd!]

let renderWidth = videoTrack.naturalSize.width
let renderHeight = videoTrack.naturalSize.height

let mainCompositionInst = AVMutableVideoComposition()
mainCompositionInst.frameDuration = CMTimeMake(value: 1, timescale: 60)
mainCompositionInst.renderScale = 1.0
mainCompositionInst.renderSize = CGSize(width: renderWidth, height: renderHeight)
mainCompositionInst.instructions = [mainInstruction]

let exporter: AVAssetExportSession = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)!
exporter.outputURL = self.template.finalURL
exporter.outputFileType = AVFileType.mov
exporter.audioMix = audioMix
exporter.shouldOptimizeForNetworkUse = true
exporter.videoComposition = mainCompositionInst
exporter.exportAsynchronously(completionHandler: {
                DispatchQueue.main.async{
                    self.exportDidFinish(session: exporter)
                }
            })


Solution

  • you just need to check the audio time.