全屏播放 YouTube 视频

Play YouTube video full screen

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

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

创建自定义 table 视图单元格。在 Interface Builder 中,将一个视图拖到您的单元格中,将 class 更改为 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;
}

这是报春花在 Swift 2.2 中的回答:

table 查看单元格:

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

表视图控制器:

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!
}