HanekeSwift: Caching file, returning disk URL to retrieve later

Hey there!

I have a requirement to cache video data, but then retrieve the URL location for that data. The reason for this is an AVAsset cannot be created using NSData, only via a URL.

A very hacky (and very unreliable) way to do it now via Haneke is to do the following:

let cache = Haneke.sharedDataCache
cache.fetch(URL: URL).onSuccess { (data) in
  // Hacky way of getting cache URL from Haneke
  let path = DiskCache(name: "shared-data", capacity: UINT64_MAX).pathForKey(URL.absoluteString!)
  let cacheURL = NSURL(fileURLWithPath: path)
}

What are your thoughts on how this should be handled? Should I forego using Haneke for this specific feature and build a disk-only cache instead?

James

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 1
  • Comments: 21 (4 by maintainers)

Most upvoted comments

I have the same issue, just for future references if somebody stuck with this, here my workaround (I’m reusing the player of SCRecorder)

let cache = Shared.dataCache
let URL = NSURL(string: "<video url>")!

cache.fetch(URL: URL).onSuccess { stream in

    let path = NSURL(string: DiskCache.basePath())!.URLByAppendingPathComponent("shared-data/original")
    let cached = DiskCache(path: path.absoluteString).pathForKey(URL.absoluteString)
    let file = NSURL(fileURLWithPath: cached)

    // so lets play the video
    let player: SCPlayer = SCPlayer()
    player.setItemByUrl(file)
    let layer = AVPlayerLayer(player: player)
    layer.frame = self.viewer.bounds
    self.viewer.layer.addSublayer(layer) // this is my view
    player.play()
}

Thank’s @jamescmartinez for show the way…

@frangeris i see… very interesting. I looked further into the issue and i noticed that the FIRST time you cal the cache onSuccess, it wont show up. if you call it again, however, it will show up. I made it so if the cache file doesnt exists, write the NSData to it, and read that. The in the future, the file will exist, and work fine

                    let URL = NSURL(string: location)!


                    self.cache.fetch(URL: URL).onSuccess { stream in    

                        let path = NSURL(string: DiskCache.basePath())!.URLByAppendingPathComponent("shared-data/original")
                        let cached = DiskCache(path: path.absoluteString).pathForKey(URL.absoluteString)
                        let file = NSURL(fileURLWithPath: cached)

                        //if there is no file, write it so it can be used in the player.
                        if !NSFileManager.defaultManager().fileExistsAtPath(cached) {
                            try! stream.writeToFile(cached, options: NSDataWritingOptions.AtomicWrite)
                        }



                        self.player = AVPlayer(URL: file)

                        let playerLayer = AVPlayerLayer(player: self.player)
                        playerLayer.frame = self.view.bounds
                        cell.videoView.layer.addSublayer(playerLayer)

                        self.player.play()

                    }

Hi guys, I tryied to do like @X901 but AVPlayer remains empty (not loading videos). If I check file existence, there is. But still, no video is displayed. Why this?