Using Haneke to cache then play MP4 files with AVPlayer

不羁的心 提交于 2019-12-21 20:56:01

问题


I'm looking for some help solving what seems like a simple problem, but I just can't seem to figure it out.

I'm using Haneke from Cocoapods to cache MP4 files from URL (on my server) and then play the cached version using AVPlayer. I'm setting up an AVPreviewLayer inside a UICOllectionViewCell and running this code. The issue is that the player won't play the video initially, but it will play it again in the future (after i scroll back to that cell in the uicollectionview)

           let getUrl = NSURL(string: location)

            cache.fetch(URL: getUrl!).onSuccess { (data) in

                // Hacky way of getting cache URL from Haneke
                let basePath = DiskCache.basePath().stringByAppendingPathComponent("shared-data/original/")
                let path = DiskCache(path: basePath, capacity: UINT64_MAX).pathForKey(getUrl!.absoluteString)
                let fileURL = NSURL(fileURLWithPath: path)

                self.player = AVPlayer(URL: fileURL)

                let playerLayer = AVPlayerLayer(player: self.player)

                playerLayer.frame = self.view.bounds

                cell.videoView.layer.addSublayer(playerLayer)

                print("Playing video")

                self.player.play()

            }

来源:https://stackoverflow.com/questions/36758309/using-haneke-to-cache-then-play-mp4-files-with-avplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!