Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play videos automatically on Tableview cell in iOS

I want to play videos on Tableview using MPMoviePlayer. So I have 10 videos and I need to load the videos on tableview. So while scrolling on tableview, don't want to play videos. Once tableview scrolling completed, current visible cell index video needs to be auto played inside the cell. How can I implement this?

See below for the code on every cell,

[self.players.view removeFromSuperview];

NSURL *videoURL = [NSURL URLWithString:str_videourl];

self.players = [[MPMoviePlayerController alloc]init];

[self.players.view setFrame:videoView.frame];

self.players.controlStyle = MPMovieControlStyleEmbedded;
self.players.shouldAutoplay = YES;
self.players.movieSourceType = MPMovieSourceTypeStreaming;
[self.players setContentURL:videoURL];
self.players.scalingMode = MPMovieScalingModeAspectFill;
self.players.view.tag=indexPath.section;

[cell.viewBase addSubview:self.players.view];

[self.players prepareToPlay];

[self.players.view setBackgroundColor:[UIColor clearColor]];
like image 499
Thangavel Avatar asked Nov 09 '22 08:11

Thangavel


1 Answers

You need to add validation in your cellForRowAtIndexPath method for checking tableview rows motion. Use following code for checking if scrolling is stopped.

if (self.tableView.dragging == NO && self.tableView.decelerating == NO)
{
    // Here comes your code for playing/resuming video
}
like image 74
Ashish P. Avatar answered Nov 14 '22 22:11

Ashish P.