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 insertTimeRange the source AVAsset of audioTrack got released. AVAssetTrack keeps weak reference to source AVAsset, so you must retain AVAsset in order to use AVAssetTrack.