Question

Another Solution for duplicating movie players

-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {

    [_moviePlayerR stop];
    [_moviePlayerR.view removeFromSuperview];
}

EDIT QUESTION

I edited the code with custom cell, it works well and no crash issue but the new problem is when the video is working, if Im scrolling the video is duplicated in another cell how could I play the video in the current cell only.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *sessionCellID = @"imageID";
    static NSString *noneID = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:noneID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:noneID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
   if( indexPath.row == 0 ) {
        TimeLineCell *cell = nil;
        cell = (TimeLineCell *)[tableView dequeueReusableCellWithIdentifier:sessionCellID];
        if( !cell ) {
            cell = [[TimeLineCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sessionCellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

        }
        cell.mainImage.tag = indexPath.section;
        dispatch_async(dispatch_get_main_queue(), ^{

            [cell.mainImage setImageWithURL:[NSURL URLWithString:[pictures objectAtIndex:indexPath.section]]
                      placeholderImage:[UIImage imageNamed:@"placeholder.png"] ];
        });
        UITapGestureRecognizer* tapu = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)];
        tapu.numberOfTouchesRequired = 1;
        [cell.mainImage setUserInteractionEnabled:YES];
        [cell.mainImage addGestureRecognizer:tapu];
        return cell;
    }
   return cell;
}

-(void)tapImage:(UITapGestureRecognizer *)gestureRecognizer {
    CGPoint tapLocation = [gestureRecognizer locationInView:self.tableView];
    NSIndexPath *tapIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];
    TimeLineCell* tappedCell = (TimeLineCell*)[[self.tableView cellForRowAtIndexPath:tapIndexPath] viewWithTag:tapIndexPath.section];
    NSURL *movieURL = [NSURL URLWithString:[videourl objectAtIndex:tapIndexPath.section]];
    MPMoviePlayerController *player =
    [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
    [player prepareToPlay];
    [player.view setFrame: tappedCell.mainImage.bounds];
    _moviePlayerR=player;
    [tappedCell.mainImage.viewForBaselineLayout addSubview: _moviePlayerR.view];
    [_moviePlayerR play];
}

I used sections and each section has rows so the image for the first row.

I written this code which it has tableView, each cell has image loading using SDWebImage and every image has UITapGesture, if the user touch any image it must play video over the current cell.

It works well, but the user is scrolling while the video is playing it make crash issue as this message:

-[MPVideoBackgroundView setImage:]: unrecognized selector sent to instance 0x175e65c0

This is the code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Main";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIImageView *showImage;
 showImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 310)];
            showImage.tag = 99;
            [cell addSubview:showImage];   
        }
    else {
        showImage = (UIImageView*)[cell viewWithTag:99];
    }
            UITapGestureRecognizer* tapu = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)];
            tapu.numberOfTouchesRequired = 1;

            showImage.tag = indexPath.section;
            [showImage setUserInteractionEnabled:YES];
            [showImage addGestureRecognizer:tapu];

 dispatch_async(dispatch_get_main_queue(), ^{

        [showImage setImageWithURL:[NSURL URLWithString:[pictures objectAtIndex:indexPath.section]]
                  placeholderImage:[UIImage imageNamed:@"placeholder.png"] ];
        });

return cell
}

-(void)tapImage:(id)sender {
    UITapGestureRecognizer *tapRec = (UITapGestureRecognizer *)sender;
    UIImageView *label = (UIImageView *)tapRec.view;
    NSURL *movieURL = [NSURL URLWithString:[videourl objectAtIndex:label.tag]];
    MPMoviePlayerController *player =
    [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
    [player prepareToPlay];
    [player.view setFrame: label.bounds];
    self.moviePlayerR=player;
    [label.viewForBaselineLayout addSubview: self.moviePlayerR.view];
    [self.moviePlayerR play];
}

For your information, MediaPlayer framework is included in the .h file and I set a property for (self.moviePlayerR) in .h and synthesize it in .m

Was it helpful?

Solution

Since the cells are reusable, the next section will have player controller added on it.You have to remove that player from label's subview. Put an NSLog in cellForRow To print label's subviews. you will come to know. Check the subviews for the view where you have added movie player like this
// view is where you have added the player's instance

 for (UIView * checkView in view.subView){
       if (checkView isKindOfClass:[MPMoviePlayerController class]){
           [checkView removeFromSuperView];
           break;
       }
    }

or else set a tag for player's instance and remove the view by checking the tag. I have not compiled the code, just wrote a sample logic.

Hope it helps :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top