我正在使用Swift-YouTube-Player库。这是link!
我在tableview中列出视频。我的问题是内存问题。我知道在加载所选的videoPlayer之前我应该清除所有的videoPlayer.clean()。
我的问题是如何在加载其他视频之前清除我应用中播放器的每个实例。
我的代码如下:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let jlist = self.searchedJoinAllAllowedInnoList , !jlist.isEmpty{
let cell = tableView.dequeueReusableCell(withIdentifier: "JoinCell", for: indexPath) as! JoinCell
cell.delegate = self
cell.indexPath = indexPath
//--
cell.videoName.text = jlist[indexPath.row].shortDesc
cell.EndDate.text = getDate(unixdate: jlist[indexPath.row].joinEnd!, timezone: "UTC")
if let videoKey = jlist[indexPath.row].teaserVideo , !videoKey.isEmpty{
print("video key:\(videoKey)")
cell.thubnailImageView.sd_setImage(with: URL(string: "https://img.youtube.com/vi/\(videoKey)/hqdefault.jpg"), placeholderImage: UIImage(named: "lcw"))
let playerVars = ["controls": "1", "playsinline": "1", "autohide": "1", "showinfo": "1", "autoplay": "0", "fs": "1", "rel": "0", "loop": "0", "enablejsapi": "1", "modestbranding": "1"]
cell.videoPlayer.playerVars = playerVars as YouTubePlayerView.YouTubePlayerParameters
cell.videoPlayer.clear()
}
return cell
}
else {
let cell = UITableViewCell()
cell.selectionStyle = .none
cell.backgroundColor = .clear
cell.textLabel?.textAlignment = .center
cell.textLabel?.textColor = UIColor.black
cell.textLabel?.text = "nodataavaiable".localized()
return cell
}
}
我的自定义播放按钮代理是:
extension JoinViewController : JoinCellButtonsDelegate {
func playButtonDelegate(at index: IndexPath) {
if let jlist = self.searchedJoinAllAllowedInnoList , !jlist.isEmpty{
if let videoKey = jlist[index.row].teaserVideo , !videoKey.isEmpty{
let cell = joinTableView.cellForRow(at: index) as! JoinCell
cell.videoPlayer.clear()
cell.videoPlayer.loadVideoID(videoKey)
cell.videoPlayer.play()
self.delayWithSeconds(2, completion: {
cell.videoPlayer.play()
cell.thubnailView.isHidden = true
})
}
}
}
}
答案 0 :(得分:0)
我不确定,但我可以尝试在最后一个闭包中使用弱单元格引用。
ALTER
并且不要忘记删除上一行:delayWithSeconds(2, completion: { [weak cell] in
cell?.videoPlayer.play()
cell?.thubnailView.isHidden = true
})
。