2011-11-30 98 views
2

我一直在玩這個遊戲一天,並且撞到了一堵磚牆。在UIScrollView中停止和啓動視頻

我想要一個UIScrollView,顯示一系列用戶可以滾動的視頻。設置UIScrollView是好的,它內的每個項目(我稱之爲MenuItems)是UiViewController的子類,它包含並管理MPMoviePlayerController等的所有設置。

但是我發現,您只能擁有一個MPMoviePlayerController在一個窗口,一次玩。

所以,我認爲處理這個最好的方法是讓每個菜單項訪問的方法,stopVideostartVideo,我會觸發每個菜單項成爲了UiScrollView的「焦點」(我已經成功地編碼委託所以它捕獲滾動事件並確定哪個頁面位於滾動視圖的中心)。

問題是,我無法解決如何訪問UiScrollView中的MenuItem對象。

我在UIScrollView委託下面的代碼來做到這一點:

- (void)scrollViewDidScroll:(UIScrollView *)sView { 

//establish what page we're on 
static NSInteger previousPage = 0; 
MenuItem *currentMenuItem; 
MenuItem *previousMenuItem; 

CGFloat pageWidth = sView.frame.size.width; 
float fractionalPage = sView.contentOffset.x/pageWidth; 
NSInteger page = lround(fractionalPage); 
if (previousPage != page) { 
    //firstly, get the previous page and stop the video 
    previousMenuItem = [sView.subviews objectAtIndex:previousPage]; 
    previousMenuItem = [previousMenuItem nextResponder]; 
    [previousMenuItem hideVideo]; 

    //[previousMenuItem release]; 

    //page has changed, get the new current page and start the video 
    currentMenuItem = [sView.subviews objectAtIndex:page]; 
    currentMenuItem = [currentMenuItem nextResponder]; 
    [currentMenuItem showVideo]; 


    //[currentMenuItem release]; 

    previousPage = page; 
} 

}

的方法showVideohideVideo是所謂的,但我得到其他錯誤(例如,我不能似乎在MenuItem內初始化MPMoviePlayerController而沒有SIGABRT錯誤)。

我在想我的方法在這裏有缺陷,或者有一個更簡單的方法?所有幫助讚賞!

非常感謝。

回答

0

遲了一點回應,但我也被困在這個,並設法擊敗了一個解決方案,所以認爲我會把它包括給其他人。

我認爲它與電影播放器​​的內存分配有關。

我包括在我.h文件中的代碼

@interface VideoInstructionViewController : UIViewController <UIScrollViewDelegate> 
{ 
    MPMoviePlayerController *player0; 
    MPMoviePlayerController *player1; 
    MPMoviePlayerController *player2;  
} 

,並從而使得電影播放器​​是有效的全球,然後就包含在我的.m文件的代碼。

player0 = [[MPMoviePlayerController alloc] initWithContentURL:contentURL]; 
player1 = [[MPMoviePlayerController alloc] initWithContentURL:contentURL]; 
player2 = [[MPMoviePlayerController alloc] initWithContentURL:contentURL]; 

其餘的代碼非常有用,我認爲這是一個可靠的方法,謝謝!