2015-02-09 59 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