Home

Awesome

ZPlayerCacher

ZPlayerCacher

<p align="center"> <a href="https://codecov.io/gh/ZhgChgLi/ZPlayerCacher" target="_blank"><img src="https://codecov.io/gh/ZhgChgLi/ZPlayerCacher/branch/main/graph/badge.svg?token=DtFM8tKJye"></a> <a href="https://github.com/ZhgChgLi/ZPlayerCacher/actions/workflows/ci.yml" target="_blank"><img src="https://github.com/ZhgChgLi/ZPlayerCacher/actions/workflows/ci.yml/badge.svg?branch=main"></a> </p>

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.

Installation

Swift Package Manager

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

Things to know

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

Integration Tools

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. :)