我正在尝试建立一个表格视图,并在每个表格视图单元格中加载视频。我目前拥有它,因此可以在视图控制器中将视频下载并保存到缓存的URL。然后,该缓存的url被提供给表视图单元控制器,在该控制器中应将视频加载到其中,但是它不起作用。
当我放置来自youtube的常规视频链接或未缓存的链接时,视频播放正常。但是,当我尝试使用此缓存的URL时,AVKit将无法播放视频。
我在这里实际下载这些视频URL:
Database.database().reference().child("posts").observe(.value, with: {
(snapshot) in
if let snapshot = snapshot.children.allObjects as? [DataSnapshot]{
self.posts.removeAll()
for data in snapshot{
print(data)
if let postDict = data.value as? Dictionary<String, AnyObject>{
let key = data.key
let post = Post(postKey: key, postData: postDict)
CacheManager.shared.getFileWith(stringUrl: post.videoLink) { result in
switch result {
case .success(let url):
post.downloadedURL = url;
case .failure(let error):
print("Error: \(error.localizedDescription)")
}
self.posts.append(post)
}
}
}
self.tableView.reloadData()
}
})
这是表格视图单元格控制器中的代码,在单元格加载时会被调用:
func configCell(post: Post, img: UIImage? = nil, userImg: UIImage? = nil) {
// let playerItem = CachingPlayerItem(url: URL(string: post.videoLink)!)
//player = AVPlayer(playerItem: playerItem)
//print(post.downloadedURL.)
let item = AVPlayerItem(asset: AVAsset(url: post.downloadedURL))
self.player = AVPlayer(playerItem: item)
self.avPlayerLayer = AVPlayerLayer(player: self.player)
self.avPlayerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
self.player.automaticallyWaitsToMinimizeStalling = false
self.postVideo.layer.addSublayer(self.avPlayerLayer)
print("hello here we go: \(self.player.error?.localizedDescription)")
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player.currentItem, queue: .main) { [weak self] _ in
self?.player?.seek(to: CMTime.zero)
self?.player?.play()
}
这是post.downloadedURL
的输出:
"file:///Users/willcohen/Library/Developer/CoreSimulator/Devices/10FE9929-E5D9-4410-B907-372A659CDFD0/data/Containers/Data/Application/DEC0896A-9F58-437D-9F43-D5545C2CE45A/Library/Caches/nnxCgXhXPkwJ0W8U"
我希望AVKit可以像处理来自互联网的常规URL一样处理此缓存的URL,但是不会将其加载到视频中。任何帮助将不胜感激。