Embedding Videos in a Tableview Cell

Play Videos in TableViewCell

If you create a custom UITableViewCell, you can add a AVPlayer object to that cell and load it up with a video (some code you can use to start with can be found in this related question).

To be honest though, do not expect responsive or fast UI in your table view if you have 7 videos playing at the same time. This is going to be very taxing on the device (in terms of CPU and battery). And each time the "more" button is touched (to add more videos), you might get even slower performance (mitigated only by if you code things in a friendly way -- e.g. where the player stops when the cell is scrolled offscreen).

How to embed AVPlayerViewController in a TableViewCell

Update

Thanks for the help Alex. After reviewing that example, I was able to add video to to the cell once I returned a UIView which contained the AVPlayer.

let videoFrame: UIView = {
let frame = UIView(frame: CGRect(x: 0, y: 95, width: screenWidth, height: 211))
let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(url: videoURL!)
let video = AVPlayerViewController()
video.player = player
video.view.frame = frame.bounds
frame.addSubview(video.view)
video.player?.play()
return frame
}()

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

addSubview(videoFrame)

Embedded YouTube video in a UIWebView fails to play

Code

var testArray = ["https://www.youtube.com/embed/W7qWa52k-nE", "https://www.youtube.com/embed/vH7i-qpoFVk", "https://www.youtube.com/embed/V_GTrL-UVmE"]

override func viewDidLoad() {
super.viewDidLoad()

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return testArray.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! WebViewTableViewCell

cell.videoWeb.allowsInlineMediaPlayback = true

let embededHTML = "<html><body><iframe src=\"\(testArray[indexPath.row])?playsinline=1\" width=\"'\(cell.videoWeb.frame.width)'\" height=\"'\(cell.videoWeb.frame.height)'\" frameborder=\"10\" allowfullscreen></iframe></body></html>"

cell.videoWeb.loadHTMLString(embededHTML, baseURL: NSBundle.mainBundle().bundleURL)
cell.videoWeb.scrollView.scrollEnabled = false

return cell
}

Screenshot

Sample Image

Test Project

test project link

how to add Videos in UITableViewCell

1. This is one way of adding video in cell

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *cellIdentifier =@"CellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}


NSURL *videoURL = [NSURL URLWithString:videoURL];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[moviePlayer setControlStyle:MPMovieControlStyleNone];
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[moviePlayer.view setFrame:CGRectMake(10.0, 0.0, 150.0 , 100.0)];
[cell.contentView addSubview:moviePlayer.view];
moviePlayer.view.hidden = NO;
[moviePlayer prepareToPlay];
[moviePlayer play];
return cell;
}

2. But the best way of adding video in cell is to subclass a UITableViewCell and add a MPMoviePlayerController as its Property

@implementation CustomVideoCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.movie = [[MPMoviePlayerController alloc] init];
self.movie.scalingMode = MPMovieScalingModeAspectFit;
[self.contentView addSubview:self.movie.view];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
self.movie.frame = CGRectMake(10, 0, self.bounds.size.width - 80, self.bounds.size.height);
}

In swift 4:

        static let tableViewCellIdentifier = "CellID"

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: tableViewCellIdentifier)
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: tableViewCellIdentifier)
}


let videoURL = URL(string: videoURL ?? "")
if let videoURL = videoURL {
moviePlayer = MPMoviePlayerController(contentURL: videoURL)
}
moviePlayer.controlStyle = .none
moviePlayer.scalingMode = .aspectFit
moviePlayer.view.frame = CGRect(x: 10.0, y: 0.0, width: 150.0, height: 100.0)
cell?.contentView.addSubview(moviePlayer.view)
moviePlayer.view.hidden = false
moviePlayer.prepareToPlay()
moviePlayer.play()
return cell!
}

......

    class CustomVideoCell {
init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

movie = MPMoviePlayerController()
movie.scalingMode = .aspectFit
contentView.addSubview(movie.view)

}

func layoutSubviews() {
super.layoutSubviews()
movie.frame = CGRect(x: 10, y: 0, width: bounds.size.width - 80, height: bounds.size.height)
}
}


Related Topics



Leave a reply



Submit