neekeetab / CachingPlayerItem

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

Issue playing url w/o extention #7

Closed yeralin closed 5 years ago

yeralin commented 6 years ago

Думаю тут для многих эта проблема возникнет, так что я по английски.

So, if I use your code as is, everything seems fine up until I try to start playing player.play().

I found out that AVURLAsset requires an extension like .mp3 in a url. In my case though, I don't use any extensions. My urls are of this form localhost:8000/stream?someId=<id>, and AVPlayer with AVURLAsset won't play the audio from such link (thanks to Apple).

Right now, I doing it a very hacky way by adding the following: let asset = AVURLAsset(url: urlWithCustomScheme.appendingPathComponent("/fake.mp3"))

And then in ResourceLoader implementation, I remove fake.mp3 path:

func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
...
guard let interceptedUrl = loadingRequest.request.url?.deletingLastPathComponent()
...

Basically adding .mp3 extention for AVURLAsset, and then removing in ResourceLoader, so that ResourceLoader could actually get my audio file.

I'm looking for a nice workaround. You have any ideas?

neekeetab commented 6 years ago

I've been asked for the solution several times already. And each time I responded with the workaround that you've described.

With the current implementation, I can't think of any "proper" way to deal with urls that don't end with an extension. The reason for this is that AVPlayerItem, that is the superclass of the CachingPlayerItem, determines the type of the file to play based on the file's url extension. If we omit an extension, there's simply no way for AVPlayerItem to tell how to treat the file.

It seems like it's a common issue. I'll leave it open for now. Duplicate of #5