sukov / cachingplayeritem Goto Github PK
View Code? Open in Web Editor NEWAn AVPlayerItem subclass that lets you stream and cache media content on iOS
License: MIT License
An AVPlayerItem subclass that lets you stream and cache media content on iOS
License: MIT License
For instance the URL looks like https://ddomain.com/myfile.mp4?token=adajah828hhy297
Will the player play it and cache it in such a way that even if the token changes it will still refer to the same file locally?
Hello! Thanks for the great snippet.
I've been struggling with figuring out why a large video (300MB) does not play after it's downloaded. Smaller size videos (10MB even 100MB) play fine. The video is a basic mp4 without moov item in the beginning so they have to be completely downloaded before they start playing. The larger video downloads successfully but then the player is stuck in waitingToPlayAtSpecifiedRate with reason noItemToPlay. Any ideas of what might be the problem?
I'm referring to caching initializers. It also takes way longer than regular AVPlayerItem to play if it's the caching case basically only stars playing when the entire file is downloaded while AVPlayerItem starts playing way way sooner.
Here's the video url for reference.
iOS 17.0.1
Xcode 15.0.1
Thanks a lot!
I want to be able to play media while caching to a file. There is an option called saveFilePath, also custom extension (I also don't know why I have to set it manually while all my url has extensions)
Now I need the library to save file at the given directory and later on when I play it again to use the local copy instead of streaming (Isn't that the importance of caching?)
My current example of implementations is:
let playerItem = CachingPlayerItem(url: url, saveFilePath: "nDNtKz3BQogGuLtg6XRI.mp4", customFileExtension: "mp4")
The error I get:
FileAttribute error: Error Domain=NSCocoaErrorDomain Code=260 "The file “nDNtKz3BQogGuLtg6XRI.mp4” couldn’t be opened because there is no such file." UserInfo={NSFilePath=nDNtKz3BQogGuLtg6XRI.mp4, NSUnderlyingError=0x283d055f0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}FileAttribute error: Error Domain=NSCocoaErrorDomain Code=260 "The file “nDNtKz3BQogGuLtg6XRI.mp4” couldn’t be opened because there is no such file." UserInfo={NSFilePath=nDNtKz3BQogGuLtg6XRI.mp4, NSUnderlyingError=0x283d055f0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
How do I get this to work. Stream while downloading, later play the downloaded file not streaming again??
when add a looper on queueplayer, crash for init
like this:
looper = AVPlayerLooper(player: queuePlayer, templateItem: loopPlayerItems.first!)
how can I resolve this?
did you attempt to integrate with
collectionview.isPrefetchingEnabled = true
extension VideoViewController: UICollectionViewDataSourcePrefetching {
func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) {
for indexPath in indexPaths {
if let cell = feedCV.cellForItem(at: indexPath) as? VideoCollectionCell {
cell.setUpPlayer()
}
}
}
func collectionView(_ collectionView: UICollectionView, cancelPrefetchingForItemsAt indexPaths: [IndexPath]) {
// Cancel any requests for data for the specified index paths.
// how can we target any rogue downloads if user jumps to another screen???
}
}
session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil) ???
and why not use AVAssetDownloadURLSession
https://developer.apple.com/documentation/avfoundation/avassetdownloadurlsession
instead of URLSession ?
Using CachingPlayerItem with code like this will make the crash happen
`
let url = URL(string: "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4")!
let playerItem = CachingPlayerItem(url: url, avUrlAssetOptions: nil)
let queuePlayer = AVQueuePlayer(playerItem: playerItem)
// Create a new player looper with the queue player and template item
self.avPlayerLooper = AVPlayerLooper(player: queuePlayer, templateItem: playerItem)
let playerLayer = AVPlayerLayer(player: queuePlayer)
playerLayer.frame = self.view.bounds
playerLayer.videoGravity = .resizeAspect
self.view.layer.addSublayer(playerLayer)
queuePlayer.play()
Crash when init CachingPlayerItem: CachingPlayerItem(url: url):
CachingPlayerItem/CachingPlayerItem.swift:185: Fatal error: CachingPlayerItem error: init(asset:automaticallyLoadedAssetKeys:) not implemented
I checked the code base, different places call the super.init, not this one.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.