我有一个可以显示图像或视频的tableview。 说到图像,我没有遇到任何问题。
但是当涉及到视频时,我在控制台上收到错误,我不明白。
错误是:"任务。< 2>完成错误 - 代码:-999"
从研究中我已经确定它应该在执行前退出。 但我真的不明白这意味着什么。
这是我的tableView Cell代码:
var post: Post? {
didSet {
updateView()
}
}
func updateView() {
captionLbl.text = post?.caption
usernameLbl.text = "Test"
if (post?.isVideo)! {
if let videoThumbUrlString = post?.videoThumbUrl {
let videoThumbUrl = URL(string: videoThumbUrlString)
postImgView.sd_setImage(with: videoThumbUrl)
}
}
if !(post?.isVideo)! {
if let photoUrlString = post?.photoUrl {
let photoUrl = URL(string: photoUrlString)
postImgView.sd_setImage(with: photoUrl)
}
}
}
func createPlayer() {
if (post?.isVideo)! {
if let videoUrlString = post?.videoUrl, let url = URL(string: videoUrlString) {
player = AVPlayer(url: url)
playerLayer = AVPlayerLayer(player: player)
playerLayer?.frame = self.postImgView.bounds
playerLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
self.postImgView.layer.addSublayer(playerLayer!)
player?.play()
activityIndicatorView.startAnimating()
playBtn.isHidden = true
}
}
}
@IBAction func playBtnPressed(_ sender: Any) {
createPlayer()
}
override func prepareForReuse() {
super.prepareForReuse()
playerLayer?.removeFromSuperlayer()
player?.pause()
activityIndicatorView.stopAnimating()
}
当我点击播放按钮时,视频播放但我收到了之前提到过的错误。 我不确定是否已连接,但活动指示灯也永远不会停止运行。
希望有人可以帮忙!
谢谢。
--------------------- UPDATE 完整的控制台日志消息是: " 2017-10-30 14:10:00.996661 + 0100 PhotoApp [92272:543501]任务。< 2>完成错误 - 代码:-999 "
答案 0 :(得分:1)
好吧,错误没有显示出什么。但尝试下一步改变:
if let videoUrlString = post?.videoUrl, let url = URL(string: videoUrlString) {
let asset = AVURLAsset(url: url) // < --
player = AVPlayer(playerItem: AVPlayerItem(asset: asset)) // < --
playerLayer = AVPlayerLayer(player: player)
playerLayer?.frame = self.postImgView.bounds
playerLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
self.postImgView.layer.addSublayer(playerLayer!)
player?.play()
activityIndicatorView.startAnimating()
playBtn.isHidden = true
}