【问题标题】:Swift Youtube Player Memory LeakSwift Youtube 播放器内存泄漏
【发布时间】: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这个问题,这样你也许可以轻松解决它...

标签: ios swift swift3 swift4


【解决方案1】:

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

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

不要忘记删除前一行:cell.videoPlayer.play()

【讨论】:

  • cell是一个局部变量,所以不会产生retain循环,也不需要标记为weak
猜你喜欢
  • 1970-01-01
  • 2016-12-15
  • 1970-01-01
  • 2020-03-19
  • 2011-10-11
  • 1970-01-01
  • 1970-01-01
  • 2012-06-08
  • 2018-09-17
相关资源
最近更新 更多