I want to play some videos in an endless loop in my collection view.
Each video is a cell.
I am using ALAsset.
I play this using AVPLayer, but I don’t download or play smoothly. Any suggestions.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
CGRect attachmentFrame = CGRectMake(2, 2, cell.frame.size.width-4, cell.frame.size.height-4);
ALAsset *asset = self.assets[indexPath.row];
UIView* subView;
if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
avPlayer = [[AVPlayer alloc] initWithURL:[[asset defaultRepresentation] url]];
avPlayer.muted = YES;
avPlayerLayer =[AVPlayerLayer playerLayerWithPlayer:avPlayer];
[avPlayerLayer setFrame:CGRectMake(0, 0, cell.frame.size.width-4, cell.frame.size.height-4)];
subView = [[UIView alloc]initWithFrame:attachmentFrame];
[subView.layer addSublayer:avPlayerLayer];
[[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
[cell.contentView addSubview:subView];
[avPlayer seekToTime:kCMTimeZero];
avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[avPlayer play];
avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:[avPlayer currentItem]];
}
}
- (void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *p = [notification object];
[p seekToTime:kCMTimeZero];
}
I also tried MPMoviePlayerController, but using a movie player, you can only play one video in a loop. Any other suggestion related to buffered video or thumbnails. I do not want to play sound in the video.
source
share