2
votes

I want to use YouTube Helper https://github.com/youtube/youtube-ios-player-helper to play YouTube videos in my app. I want to display the YTPlayerView in a table view cell, and when the video is tapped, I want it to start playing in full screen mode. However, when I tried out the YouTube helper it plays the video inline and doesn't expand to fullscreen. Is there any way of getting the video to play full screen immediately with YouTube helper?

2

2 Answers

0
votes

here is Primulaveris' answer in Swift 2.2:

table view cell:

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

Actually this is pretty easy. Here is the code for showing YTPlayerView in a table cell. Tap the YouTube thumbnail to play in full screen.

Create a custom table view cell. In Interface Builder, drag a view into your cell, change the class to YTPlayerView and hook it up to the playerView property of your cell.

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

In your view controller:

- (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;
}