1

我想構建一個UICollectionView,其中每個單元格都包含一個具有不同視頻的MPMoviePlayerController。我希望用戶能夠播放每個視頻。可能在UICollectionView中查看MPMoviePlayerController?

我知道你不能播放MPMoviePlayerController的多個實例。 Apple的文檔含糊不清是你是否可以實例化並將它們放置在視圖中 - 我閱讀它的方式,你實際上可以做到這一點。然而,我無法做到這一點,所以我最終放棄了,並嘗試了不同的技術。

相反,我嘗試使用select事件來實例化,放置並自動在收集單元格內播放視頻。作爲一個控件,我在同一個UIViewController中的集合視圖之外放置了一個額外的視圖。當我運行它時,外部視圖加載得很好 - 視頻實例化並出現第一幀。集合視圖中的所有單元格也都正確顯示。但是,當我選擇其中一個收集單元格時,外部視圖中的視頻完全消失,而集合視圖中的視頻已實例化但未能顯示。我知道它被實例化,因爲我對每個視圖的背景進行了顏色編碼,並選擇了單元格,視頻的背景出現了,但實際的視頻沒有。很混亂。

所以我想知道如果你甚至可以把一個視頻集合視圖中呢?

這裏的代碼視圖控制器:

#import "testViewController.h" 
#import <MediaPlayer/MediaPlayer.h> 
#import "tvCollectionCell.h" 

@interface testViewController() 

@property (strong) IBOutlet UIView *containerView; 
@property (strong) MPMoviePlayerController *videoPlayer; 

@end 

@implementation testViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (void)viewDidAppear:(BOOL)animated { 
    self.videoPlayer = [[MPMoviePlayerController alloc] 
         initWithContentURL: [NSURL fileURLWithPath: 
              [[NSBundle mainBundle] 
               pathForResource:@"testvideo" ofType:@"mp4"]]]; 

    CGRect videoFrame = CGRectMake(0, 0, self.containerView.frame.size.width, self.containerView.frame.size.height); 

    [[self.videoPlayer view] setFrame:videoFrame]; 

    self.videoPlayer.shouldAutoplay = FALSE; 
    self.videoPlayer.repeatMode = TRUE; 
    [self.videoPlayer prepareToPlay]; 
    [self.videoPlayer view].backgroundColor = [UIColor orangeColor]; 

    [self.containerView addSubview:[self.videoPlayer view]]; 
} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
    return 10; 
} 

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { 
    return 1; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

    tvCollectionCell *tvCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"tvCell" forIndexPath:indexPath]; 
    tvCell.backgroundColor = [UIColor purpleColor]; 

    return tvCell; 

} 

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 

    tvCollectionCell *tvCell = (tvCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath]; 

    MPMoviePlayerController *movie = [[MPMoviePlayerController alloc] 
             initWithContentURL: [NSURL fileURLWithPath: 
                  [[NSBundle mainBundle] 
                  pathForResource:@"testvideo" ofType:@"mp4"]]]; 

    CGRect videoFrame = CGRectMake(0, 0, tvCell.frame.size.width, tvCell.frame.size.height); 

    [[movie view] setFrame:videoFrame]; 

    movie.shouldAutoplay = TRUE; 
    movie.repeatMode = TRUE; 
    [movie prepareToPlay]; 
    [movie view].backgroundColor = [UIColor blueColor]; 

    [tvCell addSubview:[movie view]]; 

} 

@end 

感謝您的幫助!

+0

對此有何更新?你弄明白了嗎? – 2013-07-03 14:58:54

回答

0

您是否確定有對所有視頻的參考? 可能是MPMoviePlayerController aka videoPlayer 的唯一屬性被覆蓋,您可以嘗試分配一個數組來保存對每個視頻的引用,然後檢索該引用並播放該視頻控制器。

+0

self.videoPlayer是與單個單元格中的視頻分開實例化的控制視頻。在'didSelectItemAtIndexPath'中,你會看到每次選擇一個單元格時,新的MPMoviePlayerController被實例化的位置。 – 2013-05-03 07:57:42

相關問題