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.
1 Answers
Answers 1
This is because of MP4 format. You should consider using another format that is more stable for streaming.
0 comments:
Post a Comment