I'm creating AVMutableComposition from video and audio tracks:
func makeComposition(videoTrack: AVAssetTrack, audioTrack: AVAssetTrack) -> AVComposition {
let composition = AVMutableComposition()
/* ... */
let compositionAudioTrack = composition.addMutableTrack(withMediaType: .audio)!
try compositionAudioTrack.insertTimeRange(someRange, of: audioTrack, at: .zero) // Error!
/* ... */
return composition
}
and insertTimeRange(_:of:at:) fails with AVFoundationError -11800 AVError.code.unknown and undocumented underlying NSOSStatusError -12780. What might be the case?
Turns out, by the time I call
insertTimeRangethe sourceAVAssetof audioTrack got released.AVAssetTrackkeeps weak reference to sourceAVAsset, so you must retainAVAssetin order to useAVAssetTrack.