neekeetab / CachingPlayerItem

Play and cache media content simultaneously on your iOS device
MIT License
520 stars 89 forks source link

Video starts playing after the whole file is downloaded #12

Closed martinflorek closed 6 years ago

martinflorek commented 6 years ago

I want to start video playback as soon as possible instead of waiting for the whole file to be downloaded first.

I set the AVPLayer's automaticallyWaitsToMinimizeStalling to false and AVPlayerItem's preferredForwardBufferDuration to different values ranging from 0.1 to 2.0 but the AVPLayer always waits for the full download. I also tried AVPLayer's playImmediately(atRate: 1.0) method instead of the plain play().

The KVO observed status in the CachingPlayerItem is changed to "ready" only after the data is fully downloaded.

How can I start video playback immediately after some data is downloaded instead of waiting for the full download?

neekeetab commented 6 years ago

I didn't try it with video files so I cannot comment on this. My suggestion is for you to try playing an mp3 file with the same setup. If the behavior doesn't change, chances are you missed something in your code. If everything is ok, then feel free to provide a link to the video file or the source code, I will take a look.

And make sure you're testing it on a device, not a simulator.

martinflorek commented 6 years ago

@neekeetab thanks for the quick reply. I just tried it with mp3 audio and the playback starts almost instantly and starts before the full download. The status is "ready" after some kilobytes are downloaded and the audio starts playing. So this looks like a a video-only issue.

I tried this on a real device and both the mp3 and mp4 files are hosted on the same server with same permissions. Both HTTP and HTTPS connections behave the same.

I also tried the Big Buck Bunny video and its playback starts almost instantly and the download continues in the background. So it looks like encoding parameters issue. I will play with those now.

martinflorek commented 6 years ago

Waiting for the whole video to download before starting the playback is caused by the moov atom being at the end of the file. After moving it to the start of the file (e.g. with FFmpeg) the playback start is fast.

yazeedabalkhail commented 5 years ago

For those wondering how to achieve what @martinflorek mentioned. Just change this flag of your exporter exporter.optimizeForNetworkUse = true