Skip to content

ZPlayerCacher is a lightweight implementation of the AVAssetResourceLoaderDelegate protocol that enables AVPlayerItem to support caching streaming files.

License

Notifications You must be signed in to change notification settings

ZhgChgLi/ZPlayerCacher

Repository files navigation

ZPlayerCacher

ZPlayerCacher

ZPlayerCacher is a lightweight implementation of the AVAssetResourceLoaderDelegate protocol that enables AVPlayerItem to support caching streaming files.

Please note that while this project serves as a demonstration of AVFoundation AVAssetResourceLoaderDelegate and the use of Combine to manage data flow, the code may not be written to the highest standard of cleanliness. If you have any suggestions for improving the code, please feel free to create an issue or pull request on the repository.

Buy me a coffee ❤️❤️❤️

bmc-button

If this project has helped you, feel free to sponsor me a cup of coffee, thank you.

Installation

Swift Package Manager

  • File > Swift Packages > Add Package Dependency
  • Add https://github.com/ZhgChgLi/ZPlayerCacher.git
  • Select "Up to Next Major" with "1.0.0"

or

...
dependencies: [
  .package(url: "https://github.com/ZhgChgLi/ZPlayerCacher.git", from: "1.0.0"),
]
...
.target(
    ...
    dependencies: [
        "ZPlayerCacher",
    ],
    ...
)

CocoaPods

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '13.0'
use_frameworks!

target 'MyApp' do
  pod 'ZPlayerCacher', '~> 1.0.0'
end

Usage

let cacher: Cacher = PINCacher() // your implementation of Local Cache policy (Cacher Protocol), ref: /Sources/ZPlayerCacher/DataFetcherStrategy/Cacher/PINCacher.md
let logger = DefaultPlayerCacherLogger()
let factory = CacheableAVURLAssetFactory(cacher: cacher, logger: logger).makeCacheableAVURLAssetIfSupported(url: url)

let playerItem = AVPlayerItem(asset: asset) // than playerItem will support caching

// DefaultPlayerCacherLogger:
class DefaultPlayerCacherLogger: PlayerCacherLogger {
    var loggerLevel: PlayerCacherLevel = .info
}

// PINCacher:
public final class PINCacher: Cacher {

    static let cache: PINCache = PINCache(name: "ResourceLoader")

    private lazy var jsonDecoder = JSONDecoder()
    
    public func set(key: String, data: Data, completion: ((Error?) -> Void)?) {
        PINCacher.cache.setObjectAsync(data, forKey: key, completion: nil)
    }

    public func get(key: String) -> Data? {
        let data = PINCacher.cache.object(forKey: key) as? Data
        return data
    }

    public static func clean() {
        PINCacher.cache.removeAllObjects()
    }

    public static func setByteLimit(memoryByteLimit: UInt, diskByteLimit: UInt) {
        PINCacher.cache.memoryCache.costLimit = memoryByteLimit
        PINCacher.cache.diskCache.byteLimit = diskByteLimit
    }
}

Example

  • ZPlayerCacherExample/ZPlayerCacherExample.xcodeproj

Things to know

  • Due to limitations in the Apple iOS system, currently unsupported video formats such as HLS file format(.ts) are not supported by ZPlayerCacher.

Who is using

pinkoi

Pinkoi.com is Asia's leading online marketplace for original design goods, digital creations, and workshop experiences.

About

Other works

Swift Libraries

  • ZMarkupParser is a pure-Swift library that helps you to convert HTML strings to NSAttributedString with customized style and tags.
  • ZPlayerCacher is a lightweight implementation of the AVAssetResourceLoaderDelegate protocol that enables AVPlayerItem to support caching streaming files.
  • ZNSTextAttachment enables NSTextAttachment to download images from remote URLs, support both UITextView and UILabel.

Integration Tools

  • ZReviewTender is a tool for fetching app reviews from the App Store and Google Play Console and integrating them into your workflow.
  • ZMediumToMarkdown is a powerful tool that allows you to effortlessly download and convert your Medium posts to Markdown format.
  • linkyee is a fully customized, open-source LinkTree alternative deployed directly on GitHub Pages.

Donate

Buy Me A Coffe

If you find this library helpful, please consider starring the repo or recommending it to your friends.

Feel free to open an issue or submit a fix/contribution via pull request. :)

About

ZPlayerCacher is a lightweight implementation of the AVAssetResourceLoaderDelegate protocol that enables AVPlayerItem to support caching streaming files.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published