使用MPMoviePlayerController时无法识别的选择器

时间:2013-12-08 13:39:19

标签: ios objective-c uitableview unrecognized-selector

复制电影播放器​​的另一种解决方案

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

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

编辑问题

我使用自定义单元编辑了代码,它运行良好且没有崩溃问题,但新问题是视频工作时,如果我滚动视频在另一个单元格中重复,我怎么能只在当前单元格中播放视频

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

我使用了部分,每个部分都有行,所以第一行的图像。

我编写了这个代码,它有tableView,每个单元都使用SDWebImage加载图像,每个图像都有UITapGesture,如果用户触摸任何图像,它必须在当前单元格上播放视频。

效果很好,但是当视频正在播放时用户正在滚动它会导致崩溃问题,如下所示:

  

- [MPVideoBackgroundView setImage:]:无法识别的选择器发送到实例0x175e65c0

这是代码:

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

为了您的信息,MediaPlayer框架包含在.h文件中,我在.h中为(self.movi​​ePlayerR)设置了一个属性,并在.m

中合成它。

1 个答案:

答案 0 :(得分:0)

由于单元格可重复使用,下一部分将添加播放器控制器。您必须从标签的子视图中删除该播放器。将NSLog放在cellForRow中打印标签的子视图。你会明白的。 检查您添加了电影播放器​​的视图的子视图 // view是你添加了玩家实例的地方

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

或者为玩家的实例设置标记,并通过检查标记来删除视图。我没有编译代码,只是写了一个示例逻辑。

希望有所帮助:)