CachingPlayerItem
CachingPlayerItem copied to clipboard
NSURLConnection Error Code 1002
Hi, I am trying to use this with Firebase Storage. I have audio files stored there with URLs that look like this:
https://firebasestorage.googleapis.com/v0/b/audiomeshchat.appspot.com/o/projects%2H0wy4AwppfUSozaxeAMNGbA8Vyum1%2F0B7D5C4F-F65B-495C-A06E-35C236B5B5FC.m4a?alt=media&token=a5f7fcb3-0541-453e-8a47-55563518bbef6
My app works great with the following code:
let url = URL(string: (project?.url)!)!
let playerItem = AVPlayerItem(url: url)
player = AVPlayer(playerItem: playerItem)
player.automaticallyWaitsToMinimizeStalling = false
player.play()
But when I simply switch it to use CachingPlayerItem, the audio won't play and I get NSURLConnection error code 1002.
What's weird is the file is successfully downloaded and cached. But the audio player won't play. Is it possible to make this work with Firebase storage URLs? My security rules are set up such that without the token, files can't be accessed.
Sorry for late response. Is this issue still a case? FYI, the link is broken.
Yeah, I sanitized the link, not sure if it was necessary, but yeah, it won’t work. And I haven’t tried it since. I assume it’s still a problem but I can check.
From your code I can see that you initialize ‘playerItem’ with just a url. And your url doesn’t contain a file extension. Thus, CachingPlayerItem can’t refer the type of the file. Try using another initializer that takes custom file extension along with url.
I tried that too, and it didn't work. The URL actually does contain a file extension. The problem is the token comes after the URL as an option in the URL string:
Here's the first part of the URL: https://firebasestorage.googleapis.com/v0/b/audiomeshchat.appspot.com/o/projects%2H0wy4AwppfUSozaxeAMNGbA8Vyum1%2F0B7D5C4F-F65B-495C-A06E-35C236B5B5FC.m4a
And here are the query components, including the token: ?alt=media&token=a5f7fcb3-0541-453e-8a47-55563518bbef6
The file can't be accessed without the query components because of security rules.
Yeah, missed that part. If you provide a valid url I can take a look.
Here's a valid one:
https://firebasestorage.googleapis.com/v0/b/foodapp-600e4.appspot.com/o/projects%2F2eoifj02gpwodsjg02ssd%2F02C3E19E-B3F5-4325-80D0-CF89F6B486DD.m4a?alt=media&token=b35bfd0a-663b-4584-9cf2-1e7d69fddcd3
Works fine for me. Here's the code:
let url = URL(string: "https://firebasestorage.googleapis.com/v0/b/foodapp-600e4.appspot.com/o/projects%2F2eoifj02gpwodsjg02ssd%2F02C3E19E-B3F5-4325-80D0-CF89F6B486DD.m4a?alt=media&token=b35bfd0a-663b-4584-9cf2-1e7d69fddcd3")!
let playerItem = CachingPlayerItem(url: url)
player = AVPlayer(playerItem: playerItem)
player.automaticallyWaitsToMinimizeStalling = false
player.play()
Also I've noticed that the file you provided starts playing only when fully downloaded as opposed to, say, https://github.com/robovm/apple-ios-samples/blob/master/avTouch/sample.m4a?raw=true. You might want to take a look at this issue #12.
I have the same issue when I try to access currentItem.asset right after play started.
Thats because audioPlayer?.currentItem?.duration is zero at start so I tried to get duration by calling this
let duration = audioPlayer?.currentItem?.asset.duration.seconds
Is there other way to do that?
Are you experiencing the same crash when you try to access an asset? If it's just about getting the duration, then take into account that a file should contain certain metadata in order for you to know the duration before the file is fully loaded. Please take a look at https://github.com/neekeetab/CachingPlayerItem/issues/12. @artur-zaremba
Yes, I am. The issue is raised when I access asset immediately after start playing. I avoid the issue by checking if currentItem.status == .readyToPlay then I access the asset. Also I experienced app freeze before the error code 1002. It seems that I accessed it when initial chunk of asset was loading.
did anybody manage to solve 1002 error code ? I'm facing the same issue with the url: https://www.sample-videos.com/audio/mp3/crowd-cheering.mp3
I am also having this issue unfortunately! It only seems to happen like 15% of the time and not always the same song. Sound works initially but it stops after the 1002 error happens for the first time. I'm going to keep debugging!