I have a bunch of videos that I just want to play one after the other.
I made sure that my AVAsset
s are fine, and tried to create a sequence using the following function:
func makeSequence(from assets: [AVURLAsset], to previewComposition: AVMutableComposition) async {
var previousAssetEndTime: CMTime = .zero
for asset in assets {
do {
let assetDuration = try await asset.load(.duration)
let assetTimeRange = CMTimeRange(start: .zero, duration: assetDuration)
let mediaTypes: [AVMediaType] = [.video, .audio]
for mediaType in mediaTypes {
let assetTracks = try await asset.loadTracks(withMediaType: mediaType)
for assetTrack in assetTracks {
if let compositionTrack = previewComposition.addMutableTrack(withMediaType: mediaType, preferredTrackID: kCMPersistentTrackID_Invalid) {
try compositionTrack.insertTimeRange(assetTimeRange, of: assetTrack, at: previousAssetEndTime)
compositionTrack.preferredTransform = try await assetTrack.load(.preferredTransform)
}
}
}
previousAssetEndTime = CMTimeAdd(previousAssetEndTime, assetDuration)
} catch {
logger.debug("\(error.localizedDescription)")
}
}
}
Unfortunately, only the first video of the sequence is displayed. The total length of the composition however is correct, but the image remains black after the first video. The code runs without errors. Looking at the code, I can’t notice any mistakes. That’s why I am looking for your help! What am I missing here?
How do I create a AVMutableComposition that plays a bunch of videos, one after another?