我正在尝试通过XCDYouTubeClient库播放youtube视频,但无法播放该视频。我也参考了文档,但并没有帮助我解决问题

let playerViewController = AVPlayerViewController()
self.present(playerViewController, animated: true, completion: nil)

XCDYouTubeClient.default().getVideoWithIdentifier("tG7vx7-3sl0") { [weak playerViewController] (video: XCDYouTubeVideo?, error: Error?) in
      if let streamURLs = video?.streamURLs, let streamURL = (streamURLs[XCDYouTubeVideoQualityHTTPLiveStreaming] ?? streamURLs[XCDYouTubeVideoQuality.HD720] ?? streamURLs[XCDYouTubeVideoQuality.medium360] ?? streamURLs[XCDYouTubeVideoQuality.small240]) {
           playerViewController?.player = AVPlayer(url: streamURL)
      } else {
           self.dismiss(animated: true, completion: nil)
       }
}

谢谢

最佳答案

您可以使用以下代码。某些youtube链接没有XCDYouTubeVideoQuality.HD720视频质量。只需将其删除,即可开始播放视频。

XCDYouTubeClient.default().getVideoWithIdentifier("tG7vx7-3sl0") { [weak self] (video, error) in

   if video != nil {
     var streamURLs = video?.streamURLs
     let streamURL = streamURLs?[XCDYouTubeVideoQualityHTTPLiveStreaming] ?? streamURLs?[NSNumber(value: XCDYouTubeVideoQuality.medium360.rawValue)] ?? streamURLs?[NSNumber(value: XCDYouTubeVideoQuality.small240.rawValue)]
     if let streamURL = streamURL {
         playerViewController?.player = AVPlayer(url: streamURL)
     }
     playerViewController?.player?.play()

   } else {
      self?.dismiss(animated: true)
   }
}

09-29 23:57