Play video in UITableViewCell

I am trying to play the video in the cells themselves instead of the full screen video. For this purpose I use MPMoviePlayerController.

I determined

MPMoviePlayerController *moviePlayer;

in the implementation section

Then in cellForRowAtIndexPath:

I'm doing it

cell = [tableView dequeueReusableCellWithIdentifier:simpleTableVideoIdentifier];

            if (cell == nil){
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableVideoIdentifier];
            }
            NSDictionary *dictionary = [dataArray objectAtIndex:indexPath.section];
            NSDictionary *data = dictionary;
            if ([data[@"type_of_post"] isEqualToString:@"video"]) {
                NSString *path = data[@"video"];
                NSURL *videoURL = [NSURL URLWithString:path];
                moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
                [moviePlayer setControlStyle:MPMovieControlStyleNone];
                moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
                [moviePlayer.view setFrame:CGRectMake(10.0, 0.0, 300.0 , 400.0)];
                [cell addSubview:moviePlayer.view];
                 moviePlayer.view.hidden = NO;
                [moviePlayer prepareToPlay];
                [moviePlayer play];
            }else{
                //do something else
            }

and obviously assign him a height of heightForRowAtIndexPath:

but the point that it is MPMoviePlayerController *moviePlayer;determined globally does not stick to its cell, but continues to descend as it scrolls down. I do not know what other way to implement this, besides what I have described, which clearly does not seem to be the right way. I would really appreciate it if someone could lead me in the right direction.

thank

+5
source share
3 answers

Never add subview to a cell:

[cell addSubview:moviePlayer.view];

Add it only to the contentView:

[cell.contentView addSubview:moviePlayer.view];

, , , , ; , . , - , . , ( else ).

+8

, . UITableViewCell MPMoviePlayerController , , . :

#import CustomCell.h    
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdent"];
    if (!cell) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdent"]];
    }

    NSDictionary *dict = dataArray[indexPath.row];
    if ([dict[@"type_of_post"] isEqualToString:@"video"]) {
        NSString *path = @"http://<path to your video>";
        [cell.movie setContentURL:[NSURL URLWithString:path]];
        [cell.movie prepareToPlay];
        [cell.movie play];
        cell.movie.view.hidden = NO;
    } else {
         [cell.movie stop];
         cell.movie.view.hidden = YES;
         }

, , CustomCell.h:

@interface CustomCell : UITableViewCell
@property (nonatomic, retain) MPMoviePlayerController *movie;
@end

CustomCell.m:

@implementation CustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.movie = [[MPMoviePlayerController alloc] init];
        self.movie.controlStyle = MPMovieControlStyleNone;
        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 - 20, self.bounds.size.height);
 }
@end
+8

Instead of creating a player for each cell, you can create and overlay it on the cell by calling the following UITableViewDelegate method. You can still have a player-like image added to each cell to look like each cell is a player.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
+2
source

All Articles