Question

I'm currently writing a small application which takes a folder containing many short video files (~1mn each) and plays them like they were ONE long video file.

I've been using AVQueuePlayer to play them all one after another but I was wondering if there were an alternative to this, because I'm running into some problems:

  • there is a small but noticeable gap when the player switches to the next file
  • I can't go back to the previous video file without having to remove all the items in the queue and put them back

I'd like to be able to go to any point in the video, just as if it were a single video file. Is AVPlayer the best approach for this?

Was it helpful?

Solution

I realize that it's been about 6 years since this was asked, but I found a solution to this shortly after seeing this question and maybe it will be helpful to someone else.

Instead of using a an AVQueuePlayer, I combined the clips in an AVMutableComposition (a subclass of AVAsset) which I could then play in a normal AVPlayer.

let assets: [AVAsset] = urlsOfVideos.map(AVAsset.init)
let composition = AVMutableComposition()
let compositionVideoTrack = composition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)
let compositionAudioTrack = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid)

var insertTime = CMTime.zero
for asset in assets {
    let range = CMTimeRange(start: .zero, duration: asset.duration)
    guard let videoTrack = asset.tracks(withMediaType: .video).first,
        let audioTrack = asset.tracks(withMediaType: .audio).first else {
            continue
    }

    compositionVideoTrack?.preferredTransform = orientation!
    try? compositionVideoTrack?.insertTimeRange(range, of: videoTrack, at: insertTime)
    try? compositionAudioTrack?.insertTimeRange(range, of: audioTrack, at: insertTime)

    insertTime = CMTimeAdd(insertTime, asset.duration)
}

Then you create the player like this

let player = AVPlayer(playerItem: AVPlayerItem(asset: composition))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top