在UITableViewCell iOS中播放视频

时间:2017-09-15 10:14:34

标签: ios objective-c iphone tableview

我想在表格视图单元格中显示和播放视频。我的代码是: 在cellForRowAtIndexPath中:

 NSURL *videoURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",path,[[arrAboutUs objectAtIndex:indexPath.row] valueForKey:@"content"]]];
   MPMoviePlayerController* moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
    [moviePlayer setControlStyle:MPMovieControlStyleNone];
    moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
    [moviePlayer.view setFrame:cell.frame];
    [cell.contentView addSubview:moviePlayer.view];
    moviePlayer.view.hidden = NO;
    [moviePlayer prepareToPlay];
    [moviePlayer play];

但它显示空白。我的视频是MP4格式。

1 个答案:

答案 0 :(得分:1)

谢谢你@milan的宝贵建议。我使用AVPlayerViewController,它工作正常。代码:

    AVPlayer*player = [[AVPlayer alloc] initWithURL:videoURL];

        AVPlayerViewController* playerController = [[AVPlayerViewController alloc] init];


        playerController.player = player;
       [self addChildViewController:playerController];

        [cell.contentView addSubview: playerController.view];
        playerController.view.frame = cell.frame;

        [player play];