1

I just want a continuously looping video. I set up the player like this:

self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:someURL];
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
self.moviePlayer.shouldAutoplay = YES;
self.moviePlayer.repeatMode = MPMovieRepeatModeOne;
self.moviePlayer.view.frame = self.container.frame;
[self.container addSubview:self.moviePlayer.view];

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(moviePlayBackDidFinish:) name: MPMoviePlayerPlaybackStateDidChangeNotification
                                           object: self.moviePlayer];

- (void) moviePlayBackDidFinish:(NSNotification*)notification {

    NSLog( @"myMovieFinishedCallback: %@", notification );
    MPMoviePlayerController *movieController = notification.object;
    NSLog( @"player.playbackState = %d", movieController.playbackState );
}

The notification method is simply a hack that someone suggested here: Smooth video looping in iOS

I have two problems. The video looping still is not seamless. There is a very noticeable paused between loops. Second, the video stops looping after an arbitrary number of loops. Typically varies between 2-4 loops. This is obviously a huge problem for my app. Is the player really this buggy or am I doing something wrong?

Community
  • 1
  • 1
soleil
  • 12,133
  • 33
  • 112
  • 183
  • Yes, I believe the player is pretty buggy (particularly at the point playback is starting). That said, you might want to try @cmatsumoto's answer in the question you linked to. – Martin Kenny Jul 20 '12 at 13:02

2 Answers2

0

I have created a complete seamless looping solution for video here seamless-video-looping-on-ios. Feel free to download the example xcode app and try it out for yourself to see my approach in action. I found that MPMoviePlayerController and AVPlayer both fail to work for this type of thing.

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

I was also unable to get gapless looping using MPMoviePlayerController -- there was always at least 0.5s of black, and the occasional flash of the QuickTime logo.

However, I am able to obtain gapless looping using AVPlayer -- it takes a couple of conditions to achieve, though:

  1. Something about the encoding of my test video clip means that seeking to the beginning always causes a pause of about 0.5s at the start of each loop. Seeking to 1s into the clip with a kCMTimeZero tolerance makes it seamless. Without the explicit zero seek-tolerance, the effect is the same as seeking to the beginning of the clip.

  2. Seeking while not playing is erratic; it causes a hang on my iPhone 4, but not my iPad 3. The two alternative fixes (shown #if'ed below), are to:

    1. wait for the seek to finish before calling play again, or

    2. wait until a specific time (before the end of the clip), then restart playback at the beginning.

The following code implements those two conditions:

  self.player = [AVPlayer playerWithURL:url];

  self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
  self.playerLayer.frame = self.view.bounds;
  [self.view.layer addSublayer:self.playerLayer];

  [self.player seekToTime:CMTimeMakeWithSeconds(1, 1) toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];
  [self.player play];

#if 1
  [[NSNotificationCenter defaultCenter] addObserverForName:AVPlayerItemDidPlayToEndTimeNotification object:self.player.currentItem queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
    [self.player seekToTime:CMTimeMakeWithSeconds(1, 1) toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero completionHandler:^(BOOL finished) {
      [self.player play];
    }];
  }];
#endif

#if 0
  NSArray *times = [NSArray arrayWithObject:[NSValue valueWithCMTime:CMTimeMake(5, 1)]];
  [self.player addBoundaryTimeObserverForTimes:times queue:NULL usingBlock:^{
    [self.player seekToTime:CMTimeMakeWithSeconds(1, 1) toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];
  }];
#endif
Martin Kenny
  • 2,468
  • 1
  • 19
  • 16