Smooth video looping in iOS
Asked Answered
B

5

5

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?

Boatright answered 19/10, 2011 at 14:25 Comment(6)
Wow, no responses. I guess this must not be an easy thing to do in iOS. I am working on a workaround that involves two alternating AVPlayers, but it's just that, a workaround. Still hoping there is a way to loop a single clip smoothly.Boatright
Any luck? I'm running in to this issue right now as well. I'm getting the skip between loops as well as the video permanently freezes after 5-6 loops.Lothaire
I'm also experiencing freezing in IOS5 with setRepeatMode:MPMovieRepeatModeOne after a number of loopsFrick
Alright guys, this is getting off topic, but I fixed the weird freezing problem in IOS5 for a video in repeat mode. If you observe MPMoviePlayerPlaybackStateDidChangeNotification and simply do an NSLOG statement in the selector, the video no longer freezes. I'm filing a bug with AppleFrick
@SamBrodkin that didn't work for me :( I'd like to see a fix for this. Seems worse for MP4 than M4V.Kissie
@Boatright Did you ever get the alternating AVPlayers hack to work? I still seem to get a hiccup when I try that.Davena
F
3

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.

Fugitive answered 13/3, 2012 at 10:38 Comment(0)
N
2

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

Nevers answered 19/7, 2012 at 19:37 Comment(0)
S
1

To see an example that shows seamless looping of a background video (from an animated GIF) and switching between a pair of foreground character animations (with alpha channel), have a look at seamless-video-looping-on-ios. I tried to use AVPlayer in the past and had to give up on AVPlayer related solutions as they did not work well enough. See this SO question also iphone-smooth-transition-from-one-video-to-another.

Stereophonic answered 19/6, 2013 at 6:5 Comment(0)
A
0

I investigated the same problem as reported by the original poster (small pause in the loop that was breaking the seamlessness). By luck I had another video sample that didn't had this behavior and found the explanation/different only later:

The sound track.

I suspect a very slow sound initialisation (routing?).

Removing the sound track was the easiest solution for me (no sound needed) but I will have to dig further (audio mixing options and testing the solution that have been posted in this thread).

Eric

Atalya answered 25/8, 2013 at 15:17 Comment(0)
C
-1

To avoid the gap when the video is rewound, using multiple copies of the same asset in a composition worked well for me.

AVURLAsset *tAsset = [AVURLAsset assetWithURL:tURL];
CMTimeRange tEditRange = CMTimeRangeMake(CMTimeMake(0, 1), CMTimeMake(tAsset.duration.value, tAsset.duration.timescale));
AVMutableComposition *tComposition = [[[AVMutableComposition alloc] init] autorelease];
for (int i = 0; i < 100; i++) { // Insert some copies.
    [tComposition insertTimeRange:tEditRange ofAsset:tAsset atTime:tComposition.duration error:nil];
}
AVPlayerItem *tAVPlayerItem = [[AVPlayerItem alloc] initWithAsset:tComposition];
AVPlayer *tAVPlayer = [[AVPlayer alloc] initWithPlayerItem:tAVPlayerItem];
Coranto answered 17/12, 2014 at 12:27 Comment(1)
Link broken, also suggested solution does not work for truly seamless looping.Stereophonic

© 2022 - 2024 — McMap. All rights reserved.