Search code examples
iosswiftvideotvos

AVPlayer - play network video with separate audio URL without downloading the files first


I'm currently trying to play a video using AVPlayer that has separate sources for video and audio, so I combine them into a single AVPlayerItem as below:

let videoAsset = AVURLAsset(url: videoURL)
let audioAsset = AVURLAsset(url: audioURL)
let duration = videoAsset.duration
let composition = AVMutableComposition()

let videoTrack = composition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)
try? videoTrack?.insertTimeRange(CMTimeRange(start: kCMTimeZero, duration: duration), of: videoAsset.tracks(withMediaType: .video)[0], at: kCMTimeZero)

let audioTrack = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid)
try? audioTrack?.insertTimeRange(CMTimeRange(start: kCMTimeZero, duration: duration), of: audioAsset.tracks(withMediaType: .audio)[0], at: kCMTimeZero)

let playerItem = AVPlayerItem(asset: composition)

However the problem is that this code fetches the entire source of the video and audio over the network before it completes. Either calling 'videoAsset.duration' or videoTrack.insertTimeRange triggers this network request.

Is there a way to use AVPlayer to play these separate video and audio URLs together (MP4 video and AAC audio) without having to download the entire file first?

I've noticed that on my Mac, Quicktime Player also fetches the source file before it begins playback. However using something like VLC or IINA doesn't and playback can start immediately.

Thanks.


Solution

  • I never found a way to use AVPlayer to achieve this. Instead I used VLCKit which supports playback slaves from different sources.