Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smooth video looping in iOS

Can anyone suggest a method by which you can achieve a completely smooth and seamless looping of a video clip in iOS? I have tried two methods, both of which produce a small pause when the video loops

1) AVPlayerLayer with the playerItemDidReachEnd notification setting off seekToTime:kCMTimeZero

I prefer to use an AVPlayerLayer (for other reasons), but this method produces a noticeable pause of around a second between loops.

2) MPMoviePlayerController with setRepeatMode:MPMovieRepeatModeOne

This results in a smaller pause, but it is still not perfect.

I'm not sure where to go from here. Can anyone suggest a soultion?

like image 476
one09jason Avatar asked Oct 19 '11 14:10

one09jason


People also ask

Can you loop a video on IOS?

Tap to select the video or audio on the page. , then tap Movie or Audio. To set media to repeat, choose how you want it to play: Play in a continuous loop: Tap Loop.


2 Answers

I can concur @SamBrodkin's findings.

[[NSNotificationCenter defaultCenter]
    addObserver: self
    selector: @selector(myMovieFinishedCallback:)
    name: MPMoviePlayerPlaybackStateDidChangeNotification
    object: m_player];

and

-(void) myMovieFinishedCallback: (NSNotification*) aNotification
{
    NSLog( @"myMovieFinishedCallback: %@", aNotification );
    MPMoviePlayerController *movieController = aNotification.object;
    NSLog( @"player.playbackState = %d", movieController.playbackState );
}

fixed the non-looping issue on iOS 5 for me too.

like image 140
Mindbrix Avatar answered Sep 30 '22 00:09

Mindbrix


I just got this working on my iPad 3 running iOS 5.1.1, base SDK iOS 5.1

When setting up the movie player, set the repeat mode to MPMovieRepeatModeNone then add the notification

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

Then set up your selector to filter when the movie finishes playing

- (void)moviePlayerDidFinish:(NSNotification *)note {
    if (note.object == self.moviePlayer) {
        NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
        if (reason == MPMovieFinishReasonPlaybackEnded) {
            [self.moviePlayer play];
        }
    }
}

Apple made some large changes to how the MPMoviePlayerController handles loading movie files when they changed from iOS 4 to iOS 5, so I do not know if this method will work when they release iOS 6

like image 33
Christopher Matsumoto Avatar answered Sep 29 '22 23:09

Christopher Matsumoto