2015-02-09 61 views
0

我正在开发一个类似Vine应用程序的应用程序。我用MPMoviePlayerController在UITableviewcells中播放视频。我用MPMoviePlayerController创建了一个自定义tableview单元格,现在从bundle中加载一个样例测试视频。当单元格可见时,为了自动播放视频,我使用了下面的代码。藤般的表格视图单元格开始慢慢播放视频

- (FMVideoTableViewCell *)detectCenterCell 
{ 
    // Returns the FMVideoTableViewCell at the center of the screen. 
    // Assuming the center point to be (width/2,height/2). 

    return (FMVideoTableViewCell *)[self.tableView cellForRowAtIndexPath:[self findIndexPathForCellAtLocation:self.tableView.frame.size.width/2 and:self.tableView.frame.size.height/2]]; 
} 

上面的函数返回当前可见单元格,我在下面的scrollview委托中调用了这个函数。

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate; 

随着那我停止视频使用下面的方法的UITableView的不可见单元格播放。

- (FMVideoTableViewCell *)detectTopCell 
{ 
    // Returns the FMVideoTableViewCell which touches the top frame of the TableView. 
    // Assuming the top point to be (10,10). 

    return (FMVideoTableViewCell *)[self.tableView cellForRowAtIndexPath:[self findIndexPathForCellAtLocation:10 and:10]]; 
} 

- (FMVideoTableViewCell *)detectBottomCell 
{ 
    // Returns the FMVideoTableViewCell which touches the bottom frame of the TableView. 
    // Assuming the bottom point to be (width/2,height). 

    return (FMVideoTableViewCell *)[self.tableView cellForRowAtIndexPath:[self findIndexPathForCellAtLocation:self.tableView.frame.size.width/2 and:self.tableView.frame.size.height]]; 
} 

一切工作正常,因为我期望没有任何卡住滚动Tableview时。我现在面临的唯一问题是MPMovieplayer开始播放视频的延迟。我尝试了prepareToPlay属性来消除这个初始延迟。但是这会在滚动时在Tableview单元格中导致一些黑屏,并且还会降低Tableview滚动的平滑度。然后,我选择另一种解决方案,在桌面速度下降时开始播放视频,而无需等待Scrollview委托触发。我为此使用了以下代码。

static double prevCallTime = 0; 
    static double prevCallOffset = 0; 

    //Simple velocity calculation 
    double curCallTime = CACurrentMediaTime(); 
    double timeDelta = curCallTime - prevCallTime; 
    double curCallOffset = self.tableView.contentOffset.y; 
    double offsetDelta = curCallOffset - prevCallOffset; 
    double velocity = fabs(offsetDelta/timeDelta); 
    NSLog(@"Velocity: %f", velocity); 

    if(velocity < 500 && velocity > 50) 
    { 
     [self preLoadVideo]; 
    } 

    prevCallTime = curCallTime; 
    prevCallOffset = curCallOffset; 

scrollViewDidEndDragging仅在结束tableview滚动后才会触发。因此,要开始视频播放而不等待tableview滚动结束,我尝试了scrollViewWillEndDragging中的相同操作。但scrollViewWillEndDragging和scrollViewDidEndDragging之间的时差是可协商的。所以我尝试了另一种方法来调用tableview中的视频播放动作willDisplayCell委托。

但是,这也我以前不帮我,以减少玩家开始玩的初始时间。请帮我解决这个问题。提前致谢。

回答

1

最后我实现了藤蔓,如视频与AVPlayer更换MPMoviePlayer成功上市。与MPMoviePlayer相比,我认为AVPlayer是一个轻量级的玩家。此外,它还允许我们同时播放多个视频。

+0

嗨 你可以请分享你的代码,你如何播放像藤一样的视频,我正在尝试做同样的事情,但没有成功... – ravinder521986 2015-08-01 03:15:49

+0

嗨ravinder,我不能分享完整的代码,因为它很复杂。我会分享给你一些特定的功能,比如当单元格可见时播放,设置单元格的源代码等。你能提到你被卡住的部分吗? – 2015-08-03 04:34:50

+0

喜毗湿奴, 其实,当我设置自动播放是的,比打2-3细胞,我要玩唯一可见的细胞。 – ravinder521986 2015-08-07 04:33:37