【问题标题】:Play YouTube video full screen全屏播放 YouTube 视频
【发布时间】:2016-01-14 16:10:50
【问题描述】:

我想使用 YouTube Helper https://github.com/youtube/youtube-ios-player-helper 在我的应用中播放 YouTube 视频。我想在表格视图单元格中显示 YTPlayerView,当点击视频时,我希望它开始以全屏模式播放。 但是,当我试用 YouTube 助手时,它会内联播放视频并且不会扩展到全屏。 有什么方法可以让 YouTube 助手立即全屏播放视频?

【问题讨论】:

    标签: ios youtube youtube-iframe-api ytplayerview


    【解决方案1】:

    其实这很容易。这是在表格单元格中显示 YTPlayerView 的代码。点击 YouTube 缩略图以全屏播放。

    创建自定义表格视图单元格。在 Interface Builder 中,将视图拖到您的单元格中,将类更改为 YTPlayerView 并将其连接到您的单元格的 playerView 属性。

    #import <UIKit/UIKit.h>
    #import "YTPlayerView.h"
    @interface VideoCellTableViewCell : UITableViewCell
    @property (nonatomic, strong) IBOutlet YTPlayerView *playerView;
    @property (assign) BOOL isLoaded; 
    @end
    

    在您的视图控制器中:

    - (UITableViewCell *)tableView:(UITableView *)tableView    cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        VideoCellTableViewCell *cell = (VideoCellTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"VideoCell" forIndexPath:indexPath];
        [cell.playerView stopVideo];
        if (!cell.isLoaded) {
           [cell.playerView loadWithVideoId:@"your video ID"];
           cell.isLoaded = YES;
         }
         else { 
             // avoid reloading the player view, use cueVideoById instead
             [cell.playerView cueVideoById:@"your video ID" startSeconds:0 suggestedQuality:kYTPlaybackQualityDefault];
         }
    return cell;
    }
    

    【讨论】:

      【解决方案2】:

      这是 Primulaveris 在 Swift 2.2 中的回答:

      表格视图单元格:

      import UIKit
      class VideoCellTableViewCell: UITableViewCell {
          @IBOutlet var playerView: YTPlayerView!
          var isLoaded = false
      }
      

      TableViewController:

      override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
          var cell = (tableView!.dequeueReusableCellWithIdentifier("VideoCell", forIndexPath: indexPath!)! as! VideoCellTableViewCell)
          cell.playerView.stopVideo()
          if cell.isLoaded {
              cell.playerView.loadWithVideoId("your video ID")
              cell.isLoaded = true
          }
          else {
              // avoid reloading the player view, use cueVideoById instead
              cell.playerView.cueVideoById("your video ID", startSeconds: 0, suggestedQuality: kYTPlaybackQualityDefault)
          }
          return cell!
      }
      

      【讨论】:

        猜你喜欢
        • 2020-04-11
        • 1970-01-01
        • 2012-12-26
        • 2013-06-21
        • 1970-01-01
        • 2014-10-24
        • 2013-07-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多