如何从可折叠的 UITableViewCell 中播放视频?

How to Play Video from Within Collapsible UITableViewCell?

大家好:我已经设置了可折叠的 uitableviewcells 并试图让视频在 didSelectRowAtIndexPath 方法中播放。我可以让单元格向控制台发送打印语句,但无论我尝试让视频播放什么都不起作用!这是代码:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if(indexPath.section == 0)
    {
        if(indexPath.row == 0)
        {
            printf("hello");
        }
        else
        {
        printf("good bye");
        }
    }
    else if(indexPath.section == 1)
    {
        if(indexPath.row == 0)
        {
            printf("hello");
        }
        else
        {
            printf("good bye again");
        }
    }
}

你能告诉我要播放本地视频文件需要做什么吗?显然我需要 Objective C 的帮助!我试图以编程方式创建一个 AVPlayerViewController,然后使用 [self presentviewcontroller] 方法,但我总是收到以下错误(如果我使用 prepareforSegue,这也是同样的错误):

  NSString *path = [[NSBundle mainBundle] pathForResource:@"PrelimFig1" ofType:@"m4v"];
        _videoURL = [NSURL fileURLWithPath:path];
        NSLog(@"I want to log: %@", _videoURL);
        AVPlayerViewController *avp = [[AVPlayerViewController alloc]init];
        AVPlayer *player = [AVPlayer playerWithURL:_videoURL];
        avp.player = player;

        [self presentViewController:avp animated:YES completion:nil]
        [player play];

提前感谢您的帮助!

你可以这样做

   NSURL *videoURLLocal = [[NSBundle mainBundle]URLForResource:@"PrelimFig1" withExtension:@"m4v"];


    AVPlayerViewController *avp = [[AVPlayerViewController alloc]init];
    AVPlayer *player = [AVPlayer playerWithURL:videoURLLocal];
    avp.player = player;
    [self presentViewController:avp animated:YES completion:^{
        [player play];
    }];

       NSURL *videoURLLocal = [[NSBundle mainBundle]URLForResource:@"PrelimFig1" withExtension:@"m4v"];

    // create an AVPlayer
    AVPlayer *player = [AVPlayer playerWithURL:videoURLLocal];

    // create a player view controller
    AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
    controller.player = player;
    [player play];

    // show the view controller
    [self addChildViewController:controller];
    [self.view addSubview:controller.view];
    controller.view.frame = self.view.frame;