BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

SOLVED: AVPlayer streaming from downloading file

Forums > Swift

I am currently downloading and caching a video file with the following CacheManager:

public enum Result<T> {
    case success(T)
    case failure

class CacheManager {

    static let shared = CacheManager()
    private let fileManager = FileManager.default
    private lazy var mainDirectoryUrl: URL = {

    let documentsUrl = self.fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first!
        return documentsUrl

    func getFileWith(stringUrl: String, completionHandler: @escaping (Result<URL>) -> Void ) {
        let file = directoryFor(stringUrl: stringUrl)

        //return file path if already exists in cache directory
        guard !fileManager.fileExists(atPath: file.path)  else {
        } {

            if let videoData = NSData(contentsOf: URL(string: stringUrl)!) {
                videoData.write(to: file, atomically: true)

                DispatchQueue.main.async {
            } else {
                DispatchQueue.main.async {

    private func directoryFor(stringUrl: String) -> URL {
        let fileURL = URL(string: stringUrl)!.lastPathComponent
        let file = self.mainDirectoryUrl.appendingPathComponent(fileURL)

        return file

And injecting the AVPlayer to my View in this way:

CacheManager.shared.getFileWith(stringUrl: video.url) { result in

                switch result {
                case .success(let url):
                    self.player = AVPlayer(url: url)
                case .failure:

But how could I stream the file file while it is downloading without waiting it for complete?


If you pass the remote URL directly to the AVPlayer? Does it do anything? I remember playing remote videos in similar fashion without the need for download.


@nemecek-filip it streams the video but the CacheManager class is needed for caching the video and prevent it from downloading over and over again. Furthermore streaming it directly from a Firebase Storage link with AVPlayer causes a incredibly high bandwidth usage (I do not know why).


I see. Maybe this could help -


@nemecek-filip i tried to implement CachingPlayerItem but for a reason unknown to me it always download the entire .mp4 file from the database storage...

This is my simple implementation but I think that the problem is on CachingPlayerItem class presented on the Git repo:

let videoUrl: URL = URL(string: video.url)!
let playerItem = CachingPlayerItem(url: videoUrl)
self.player = AVPlayer(playerItem: playerItem)
player?.automaticallyWaitsToMinimizeStalling = false


I solved the problem integrating ChachingPlayerItem ( ) with Cache ( )

import AVKit
import Cache

class VideoPlayerWorker {

    private var player: AVPlayer!
    let diskConfig = DiskConfig(name: "DiskCache")
    let memoryConfig = MemoryConfig(expiry: .never, countLimit: 10, totalCostLimit: 10)

    lazy var storage: Cache.Storage<String, Data>? = {
        return try? Cache.Storage(diskConfig: diskConfig, memoryConfig: memoryConfig, transformer: TransformerFactory.forData())

    /// Plays a video either from the network if it's not cached or from the cache.
    func play(with url: URL) -> AVPlayer {
        let playerItem: CachingPlayerItem
        do {
            let result = try storage!.entry(forKey: url.absoluteString)
            // The video is cached.
            playerItem = CachingPlayerItem(data: result.object, mimeType: "video/mp4", fileExtension: "mp4")
        } catch {
            // The video is not cached.
            playerItem = CachingPlayerItem(url: url)

        playerItem.delegate = self
        self.player = AVPlayer(playerItem: playerItem)
        self.player.automaticallyWaitsToMinimizeStalling = false
        return self.player


// MARK: - CachingPlayerItemDelegate
extension VideoPlayerWorker: CachingPlayerItemDelegate {
    func playerItem(_ playerItem: CachingPlayerItem, didFinishDownloadingData data: Data) {
        // Video is downloaded. Saving it to the cache asynchronously.
        storage?.async.setObject(data, forKey: playerItem.url.absoluteString, completion: { _ in })



So you get it to work?

How you build it, can you give more information? What kind of structure is in the app?


Super helpful stuff. Just one gripe is I don't get how to get the delegate extension code to run before starting the player. I end up having to tap on an audio twice for it to play; I guess this means that the AVPlayer can only play a cached item for whatever reason. I am sure I am messing something up somewhere in the usage of the delegate. Any pointers would be appreciated.


can you share full code?


Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.