将视频播放器创建到UICollectionViewCell

时间:2015-10-29 10:34:06

标签: ios objective-c cocoa-touch uicollectionview uikit

我有UICollectionView,每个单元格都有视频,当用户点击该视频时,它应该开始播放。这就是我试过的:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
...
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

    dispatch_async(queue, ^{
        cell.playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:cell.videoUrl]];

        dispatch_sync(dispatch_get_main_queue(), ^{

            cell.player = [AVPlayer playerWithPlayerItem:cell.playerItem];
            cell.playerLayer = [AVPlayerLayer playerLayerWithPlayer:cell.player];
            cell.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

            cell.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
            cell.playerLayer.frame = CGRectMake(0, 0, 320, 230);
            [cell.videoView.layer addSublayer:cell.playerLayer];

        });
    });
...
}

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

    PostCollectionViewCell *cell = (PostCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];

...
   [cell.player play];


}

所以问题是,当我在视频中没有发生任何事情时,它没有播放,videoView保持白色。我在这里错过了一些东西吗?

0 个答案:

没有答案