【发布时间】:2018-01-29 08:01:40
【问题描述】:
我正在使用 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
})
}
}
}
}
【问题讨论】:
-
我建议profile这个问题,这样你也许可以轻松解决它...