【问题标题】:Playing a Video in UITableView when it is completely visible完全可见时在 UITableView 中播放视频
【发布时间】:2014-05-23 11:52:13
【问题描述】:

嗨,当单元格完全可见时,我想在 UITableView 中播放视频 URL。 怎么可能?

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *simpleTableIdentifier = @"SimpleTableCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

     if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}



 //Play the movie now
NSURL *videoURL =[NSURL fileURLWithPath:myURL];
MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
videoPlayerView.moviePlayer.fullscreen=TRUE;

[self presentMoviePlayerViewControllerAnimated:videoPlayerView];
[videoPlayerView.moviePlayer play];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
 return cell;

 }

【问题讨论】:

    标签: ios objective-c video uitableview


    【解决方案1】:

    您可以在

    上准备视频
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    

    在 cellForRowAtIndexPath 方法中,通过比较索引路径来检查当前单元格是否可见

    NSArray *visiblePaths = [tableView indexPathsForVisibleRows];
    

    如果是,则播放它,如果不停止播放。根据视频和视频数量,您当然可以使用一些缓存机制扩展此方法。如果您觉得在 willDisplayCell 上加载视频会产生延迟,您可以预加载所有视频。

    一个帮助函数,用于查找单元格是否可见。您可以在 cellForRowatIndexpath 中调用此函数。

    - (BOOL)isIndexPathVisible:(NSIndexPath*)indexPath
    {
    NSArray *visiblePaths = [self.tableView indexPathsForVisibleRows];
    
    for (NSIndexPath *currentIndex in visiblePaths)
    {
        NSComparisonResult result = [currentIndex compare:currentIndex];
    
        if(result == NSOrderedSame)
        {
            NSLog(@"Visible");
            return YES;
        }
    }
    
    return NO;
    }
    

    【讨论】:

    • 我没有得到正确的想法,你能解释一下吗
    • 我假设您想在用户像新的 Facebook 应用程序一样滚动 tableview 时开始播放电影?在这种情况下,您需要创建一个带有嵌入式电影播放器​​的 UITableViewCell 的子类。您提到的代码它是全屏显示的原生电影播放器​​。
    • 是的,我会继承它,我想要同样的东西,比如 instagram 或 vine。请告诉我当单元格完全可见时如何在 cellForRowatIndexpath 中打印(NSLog)内容
    猜你喜欢
    • 1970-01-01
    • 2020-04-06
    • 2014-02-05
    • 2014-05-09
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多