【问题标题】:Play YouTube video full screen全屏播放 YouTube 视频
【发布时间】:2016-01-14 16:10:50
【问题描述】:
【问题讨论】:
标签:
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!
}