How to insert multiple AVAssets into AVMutableCompositionTrack with silence in between?

Hi,

I'm recording videos frame by frame and occasionally a sound plays (from an MP3 asset). I want to composite these sounds into the video at the correct timings. But this doesn't work. Really pulling my hair out here. I've tried everything, including adding one after another and then inserting silence in between (allegedly this pushes subsequent clips back) but nothing works.

Here, _currentTime is the current time according to the video frames added, which are added at 20Hz.

You can see I am adding silence long enough to cover the time from the end of the last audio clip to now, plus extra padding to contain the audio we are about to add. Doesn't matter if I remove this, it just doesn't work. Sometimes I can get two pieces of audio to play but never a third and usually, only the first audio plays, and then nothing after.

I'm completely stumped.

    func addFrame(_ pixelBuffer: CVPixelBuffer) {
        guard CGSize(width: pixelBuffer.width, height: pixelBuffer.height) == _outputSize else { return }

        let frameTime = CMTimeMake(value: Int64(_frameCount), timescale: _frameRate)

        if _videoInput?.isReadyForMoreMediaData == true {
            _pixelBufferAdaptor?.append(pixelBuffer, withPresentationTime: frameTime)
            _frameCount += 1
            _currentTime = frameTime
        }
    }

    func addMP3AudioClip(_ audioData: Data) async throws {
        let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString + ".mp3")
        try audioData.write(to: tempURL)

        let asset = AVAsset(url: tempURL)
        let duration = try await asset.load(.duration)
        let audioTrack = try await asset.loadTracks(withMediaType: .audio).first!

        let currentAudioTime = _currentTime.convertScale(duration.timescale, method: .default)
        _audioTrack?.insertEmptyTimeRange(CMTimeRangeFromTimeToTime(start: _lastAudioClipEndTime, end: currentAudioTime))
        _audioTrack?.insertEmptyTimeRange(CMTimeRangeFromTimeToTime(start: currentAudioTime, end:  CMTimeAdd(currentAudioTime, duration)))

        let timeRange = CMTimeRangeMake(start: .zero, duration: duration)
        try _audioTrack?.insertTimeRange(timeRange, of: audioTrack, at: currentAudioTime)

        _lastAudioClipEndTime = CMTimeAdd(currentAudioTime, duration)
        try FileManager.default.removeItem(at: tempURL)

        _audioClipTimeRanges.append(CMTimeRangeMake(start: _currentTime, duration: duration))
    }

Thank you,

-- B.

How to insert multiple AVAssets into AVMutableCompositionTrack with silence in between?
 
 
Q