I tried create loop by AVQueuePlayer, this method has delays between end and start play.
for looping AVQueuePlayer i use this code:
1 2 3 4 5 6 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.mPlayerItem]; [self player].actionAtItemEnd = AVPlayerActionAtItemEndNone; |
and playerItemDidReachEnd
1 2 3 4 5 | - (void)playerItemDidReachEnd:(NSNotification *)notification { AVPlayerItem *p = [notification object]; [p seekToTime:kCMTimeZero]; } |
Another solution without hiccups/delays based on AVMutableComposition!
Easily solution, I use one AVURLAsset for inserting to composition. No memory leaks and warnings, because used just one object.
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | - (AVAsset*) makeAssetComposition { int numOfCopies = 25; AVMutableComposition *composition = [[[AVMutableComposition alloc] init] autorelease]; AVURLAsset* sourceAsset = [AVURLAsset URLAssetWithURL:mURL options:nil]; // calculate time CMTimeRange editRange = CMTimeRangeMake(CMTimeMake(0, 600), CMTimeMake(sourceAsset.duration.value, sourceAsset.duration.timescale)); NSError *editError; // and add into your composition BOOL result = [composition insertTimeRange:editRange ofAsset:sourceAsset atTime:composition.duration error:&editError]; if (result) { for (int i = 0; i < numOfCopies; i++) { [composition insertTimeRange:editRange ofAsset:sourceAsset atTime:composition.duration error:&editError]; } } return composition; } |
Play composition:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | AVAsset *composition = [self makeAssetComposition]; // create an AVPlayer with your composition AVPlayer* mp = [AVPlayer playerWithPlayerItem:[AVPlayerItem playerItemWithAsset:composition]]; // Add the player to your UserInterface // Create a PlayerLayer: AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:mp]; // integrate it to your view. Here you can customize your player (Fullscreen, or a small preview) [[self window].layer insertSublayer:playerLayer atIndex:0]; playerLayer.frame = [self window].layer.bounds; playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; //And finally play your video: [mp play]; |
Any questions to comments please.
没有评论:
发表评论