| | |
| | | private var disposeBag = DisposeBag() |
| | | private var defaultItems = [HomeTopMenuItem]() |
| | | private var clouse:((HomeTopMenuItem)->Void)? |
| | | |
| | | private let session = URLSession.shared |
| | | private var aVPlayerLayer:AVPlayerLayer? |
| | | |
| | | override func awakeFromNib() { |
| | |
| | | } |
| | | |
| | | if let videoUrl = model.backUrl,videoUrl.isEmpty == false{ |
| | | setPlayer(url: URL(string: videoUrl)!) |
| | | checkCacheAudio(from: URL(string: videoUrl)!) {[unowned self] state, url in |
| | | self.setPlayer(url: url) |
| | | } |
| | | return |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | // 下载视频并缓存,如果没有缓存,原路返回并异步下载 |
| | | internal func checkCacheAudio(from url: URL, completion: @escaping (Bool,URL) -> Void) { |
| | | |
| | | let cacheDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!.appendingPathComponent("bgVideos") |
| | | |
| | | let videoCacheUrl = cacheDirectory.appendingPathComponent(url.lastPathComponent.jq_md5String() + "." + url.pathExtension) |
| | | |
| | | // 检查缓存中是否已存在文件 |
| | | if FileManager.default.fileExists(atPath: videoCacheUrl.path) { |
| | | print("启用缓存") |
| | | completion(true,videoCacheUrl) |
| | | return |
| | | }else{ |
| | | completion(false,url) |
| | | print("没有缓存:执行下载") |
| | | } |
| | | |
| | | // 使用URLSession下载视频 |
| | | let downloadTask = session.downloadTask(with: url) { |
| | | tempLocalUrl, response, error in |
| | | print("执行下载任务") |
| | | if let tempLocalUrl = tempLocalUrl, error == nil { |
| | | do { |
| | | let temp = videoCacheUrl |
| | | |
| | | if !(FileManager.default.fileExists(atPath: cacheDirectory.path)){ |
| | | try FileManager.default.createDirectory(at: cacheDirectory, withIntermediateDirectories: true) |
| | | } |
| | | |
| | | try FileManager.default.moveItem(at: tempLocalUrl, to: temp) |
| | | } catch let e { |
| | | print("视频缓存失败:catch:\(e)") |
| | | |
| | | |
| | | |
| | | } |
| | | } else { |
| | | print("视频缓存失败:\(error?.localizedDescription ?? "")") |
| | | } |
| | | } |
| | | downloadTask.resume() |
| | | } |
| | | |
| | | func resetItems(_ items:[HomeTopMenuItem]){ |
| | | defaultItems.removeAll() |
| | | defaultItems = items |