Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 229
weiran Avatar asked Dec 23 '17 15:12

weiran


1 Answers

This is because of MP4 format. You should consider using another format that is more stable for streaming.

like image 67
Alexander Volkov Avatar answered Oct 15 '22 17:10

Alexander Volkov