gpt4 book ai didi

ios - Swift Youtube 播放器内存泄漏

转载 作者:行者123 更新时间:2023-11-28 07:55:01 25 4
gpt4 key购买 nike

我正在使用 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
})

}
}
}
}

最佳答案

我不确定,但我可以尝试在最后一个闭包中使用弱单元格引用。

delayWithSeconds(2, completion: { [weak cell] in
cell?.videoPlayer.play()
cell?.thubnailView.isHidden = true
})

并且不要忘记删除上一行:cell.videoPlayer.play()

关于ios - Swift Youtube 播放器内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48496797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com