eroscai / SZAVPlayer

SZAVPlayer is a lightweight audio/video player library, based on AVPlayer and AVAssetResourceLoaderDelegate, pure-Swift. Support cache and video image output.
MIT License
228 stars 32 forks source link

Player plays from remote after video is cached #8

Closed alexeyndru closed 3 years ago

alexeyndru commented 3 years ago

Situation: player managed to download and cache the video. When I play again the video, it appears to load it from remote url and not from the cache. If I disable internet connection, the video is played from cache flawlessly.

What should I change so a cached video is always played and not the remote one?

Thanks

eroscai commented 3 years ago

May be the current video does not fully cached, and the remote request currently being loaded is belong to another range. Which range request is triggered is completely controlled by Apple. You can add breakpoint to the addRemoteRequest function in the SZAVPlayerDataLoaderOperation class to view the current range that needs to be requested.

alexeyndru commented 3 years ago

Thank you for your response. My videos are short and they get completely downloaded. Also it doesn't enter addRemoteRequest function. Instead, it goes through the cache. Still, when I look at network activity, I see data fetched from server as soon as I start to play the video, and I see how the video is stalling for a little bit as though it is fetching data from server. But if I stop the internet, videos will play instantly and from cache. Any other clues?

I forgot to tell you that the library is amazing otherwise. Thank you for the effort, as it is not trivial at all.

eroscai commented 3 years ago

You can check whether the ContentInfo has been marked as expired, you can add a break point to this line. https://github.com/eroscai/SZAVPlayer/blob/786b5337cae31ba1225514329b3d4b96d49bbc2e/Sources/Classes/SZAVPlayerAssetLoader.swift#L87

Currently if the interval between two requests exceeds 1 hour, it will be marked as expired, and then new ContentInfo will be requested. If the request is successful, the ContentInfo will be updated to the latest. If it failes for some reason, it will still try to use the locally existing ContentInfo, regardless of whether is is marked as expired. https://github.com/eroscai/SZAVPlayer/blob/786b5337cae31ba1225514329b3d4b96d49bbc2e/Sources/Classes/SZAVPlayerAssetLoader.swift#L136

If this is not the cause of the problem, you can set your short video url to the DEMO project to test whether it is normal. If it is not, please send me these links, and i will test it when i'm free.

alexeyndru commented 3 years ago

I tested the Demo with my videos and they appear to work correctly. What i am trying to achieve in my app is preloading the videos. So I have multiple players which i setup initially with videos URLs. You know how in TikTok when user scrolls the next video plays instantly. Like in tik tok, my players play each at a time, but I make them download in advance the videos. In your demo you switch videos on the same player. After playing them all. when I replay they play from disk, not server. I am unable to achieve this so far. Will keep trying.

alexeyndru commented 3 years ago

Managed to solve my problem. The library works perfectly, it was something else. I was making a snapshot of the first frame of video and that was from remote url. Thanks again!