首页 > 解决方案 > 加载 youtube 视频后获取视频时长

问题描述

我正在做一个项目,我将 youtube 视频列表加载到 UITableView。至于加载和播放它们,我使用 YouTube-Player-iOS-Helper pod。根据我的理解,我使用“playerViewDidBecomeReady”委托方法来确定视频是否已加载,一旦调用,我将使用视频持续时间更新我的标签。但是由于某种原因,它不会一直更新。我的代码如下。我错过了什么

func playerViewDidBecomeReady(_ playerView: YTPlayerView) {
    print("player is ready to play")

    self.updateLabel(cell:cell,videoView:YTPlayer)
}

 static func updateLabel(cell:UITableViewCell,videoView:YTPlayerView) {

    var videoView = cell.viewWithTag(TABLE_CELL_TAGS.webView) as!  YTPlayerView!
    let durationSecs = String(describing: videoView?.duration())
    var time = videoView?.duration()
    cell.textLabel.text = time
}

我的 UITableViewDelegates 如下

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCell(withIdentifier:Utils.isIpad() ? "iPadVideosCell":"videosCell", for: indexPath)

  self.cell = cell

  var videoView = cell.viewWithTag(5) as!  YTPlayerView!
  self.YTPlayer = videoView
  videoView?.delegate = self
  return cell
 }  

标签: iosswiftswift3youtube-api

解决方案


推荐阅读