neekeetab / CachingPlayerItem

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

How to play a cached file? #10

Closed agordeev closed 6 years ago

agordeev commented 6 years ago

First of all, thanks for this useful class. However it's unclear from the README how to play mp3 from cache rather from the web after the file is cached? After relaunching the app I try to play the same url, but it starts downloading the file again.

Does cache mean memory cache only?

neekeetab commented 6 years ago

What CachingPlayerItem does is provides you with an opportunity to store the downloaded data used for playback (opposed to AVPlayerItem which doesn't expose the data). You can, for example, write the data to a local file so the next time you need it, you don't need to redownload it. In other words, you're supposed to manage caching by yourself.

neekeetab commented 6 years ago

I would recommend you use Cache for caching. You can specify URL as a key and Data as value.

michaelevensen commented 5 years ago

Hi @neekeetab ! I think this looks great and I am using CachingPlayerItem with Cache, but somehow CachingPlayerItem is modifying the URL so I can't use it as a key given I get this strange schema in front of the URL so matching against it to check my loading URL before reading from cache or not doesn't work. Is there a way to get the original absoluteString for the AVURLAsset loaded?

neekeetab commented 5 years ago

Hi @michaelevensen! The original url is stored in the CachingPlayerItem's url property. Don't rely on AVURLAsset's url – it's modified to make AVFoundation use a custom loader.

michaelevensen commented 5 years ago

Got it! But it seems like that property (URL) is marked as private. Any reason for that? Or am I just missing something here?

neekeetab commented 5 years ago

There are two initializers. If you initialize CachingPlayerItem with Data (that you usually get from the cache), the url peoperty contains a dummy path – an implementation detail. As long as you’re certain that you initialized CachingPlayerItem with a url, you can expose this property.

michaelevensen commented 5 years ago

There are two initializers. If you initialize CachingPlayerItem with Data (that you usually get from the cache), the url peoperty contains a dummy path – an implementation detail. As long as you’re certain that you initialized CachingPlayerItem with a url, you can expose this property.

And how would you recommend I go about exposing the property if I want to retain CachingPlayerItem and related logic within it's own file? Just change marking of URL to public? The specific scenario I need is be able to query (player.currentItem().asset as? AVURLAsset).url.absoluteString and match this against plain URL strings later but given how CachePlayerItem works now that URL is modified.

neekeetab commented 5 years ago

I guess best thing to do would be to subclass from CachingPlayerItem and override/create your own initializers and store the original url somewhere in your own variable within the subclass.