3

I am trying to play a video clip that loops indefinitely. I am doing this the way Apple recommends; by setting up a notification that gets triggered by AVPlayerItemDidPlayToEndTimeNotification:

@property(nonatomic) AVPlayer *videoPlayer;
@property(nonatomic) AVPlayerItem *videoPlayerItem;

-(void)loadVideo
{
    NSURL *url = [[NSBundle mainBundle] URLForResource:filename withExtension:extension];
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
    NSString *tracksKey = @"tracks";

    [asset loadValuesAsynchronouslyForKeys:@[tracksKey] completionHandler:
    ^{
        dispatch_async(dispatch_get_main_queue(),
        ^{
            NSError *error;
            AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey error:&error];

            if (status == AVKeyValueStatusLoaded)
            {
                [self setVideoPlayerItem:[AVPlayerItem playerItemWithAsset:asset]];
                [videoPlayerItem addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusContext];
                [[NSNotificationCenter defaultCenter] addObserver:self
                                                         selector:@selector (videoPlayerItemDidReachEnd:)
                                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                                           object:videoPlayerItem];
                [self setVideoPlayer:[AVPlayer playerWithPlayerItem:videoPlayerItem]];
                [scenePlayerView setVideoPlayer:videoPlayer];
            }
        });
    }];
}

When triggered, this calls my method to effectively rewind and play the clip again:

-(void)videoPlayerItemDidReachEnd:(NSNotification *)notification
{
    [videoPlayerItem seekToTime:kCMTimeZero];
    [videoPlayer play];
}

The problem is, there is a brief but visible pause in the video playback every time it hits this method and loops back to the beginning.

The video clips are H.264 and have been tested in other players to ensure that they have no visible "skips" in their content. This is all happening under iOS6 both in the Simulator and on an iPad2 and iPad3.

What am I doing wrong?

todd412
  • 1,308
  • 2
  • 17
  • 24
  • Don't have much experience with AVPlayer. Just a thought. How about using AVQueuePlayer and adding the same video as multiple times? – iDev Nov 17 '12 at 08:19
  • I actually tried that- AVQueuePlayer throws an error when you try to include the same PlayerItem in the Queue more than once. – todd412 Nov 17 '12 at 16:52
  • This answer worked for me - http://stackoverflow.com/a/26401680/2147976 – codeburn Apr 07 '15 at 11:56

3 Answers3

2

You have two possible approaches to get a really professional looking loop with no glitches (AVPlayer by itself will not work). First, you could take your original video and decode it to a series of RGB frames (images). Then encode a new h.264 video and feed the frames into a longer h.264 video that is much longer than the first. For example, you might encode a 10 second looping clips 6 times to make a clip that last one minute or 6 * 5 to make a clip that would play for 5 minutes without glitching when the loop restarts. Then, open the longer clip and play it with AVPlayer and it will not glitch for a period of time.

The second approach would be to use an existing library that already handles seamless looping. You can take a look at my library, see my answer to basically the same question iphone-smooth-transition-from-one-video-to-another. The character in the example at the linked answer uses a repeating "loop" of one video clip that loops over and over when no buttons have been pressed.

Community
  • 1
  • 1
MoDJ
  • 4,309
  • 2
  • 30
  • 65
1

Unfortunately it appears this actually is not currently possible using AVPlayer as it stands- there is an unavoidable hiccup upon beginning playback in an AVPlayer in every case. So "messier" hacks appear to be the only way to solve this, using multiple AVPlayers, etc.

todd412
  • 1,308
  • 2
  • 17
  • 24
  • 1
    Has anyone gotten the multiple players hack to work? I still get a hiccup when I alternate 2 players. – Tylerc230 Nov 26 '12 at 18:24
0

I managed to get it working with two AVPlayer, two AVPlayerLayer added to two different views (I've tried add to the same view and the upper one get stuck once in a while), and switch alpha value between these two views.

sbhhbs
  • 625
  • 4
  • 15