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

时间:2018-05-16 03:19:35

标签: objective-c uitableview avplayer avkit

全部:我已经设置了可折叠的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];

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

你可以这样做

   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;