Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to repeat a scene in a video with MPMoviePlayerController in iOS 5?

I would like to reiterate several scenes from a video several times.
In iOS 4, I could implement it already and it works. In iOS 5, the code no longer works the same way.

Here's an example:
Scene 1: Starts at 15 seconds to 30 seconds.
Scene 1 is repeated 5 times.
Scene 2: Starts at 45 seconds to 55 seconds.
Scene 2 is repeated 3 times.

In iOS 5 scene 1 is repeated 5 times. Scene 2 will not play anymore.

That's how I solved it for iOS 4:

- (void)initMoviePlayer
{
    // Some other stuff…

   iterations = 5;

   NSDictionary *currentScene = [allScenes objectAtIndex:currentSceneIndex];

   moviePlayer.repeatMode = MPMovieRepeatModeOne;
   // Scene start time
   moviePlayer.initialPlaybackTime = [[currentScene objectForKey:@"StartTime"] intValue];
   // Scene end time
   moviePlayer.endPlaybackTime = [[currentScene objectForKey:@"EndTime"] intValue];

   [moviePlayer play];
}

// Called by MPMoviePlayerPlaybackStateDidChangeNotification
- (void)playerStateChanged:(NSNotification*)notification 
{
    // Some other stuff…

    if (counter == iterations)
    {
        currentSceneIndex++;

        // Stop movie
        [moviePlayer stop];

        // Init movie player with next scene
        [self initMoviePlayer];
    }
    else
    {
        counter++;

        // Scene will repeat because of MPMovieRepeatModeOne
    }
}
like image 567
Artur Machura Avatar asked Oct 19 '11 12:10

Artur Machura


1 Answers

I suggest you to try the AVPlayer Class (here’s the documentation from Apple). This class allows you to use methods like addBoundaryTimeObserverForTimes:queue:usingBlock: and addPeriodicTimeObserverForInterval:queue:usingBlock: both interesting for your purpose.

like image 52
Jorge Ramos Avatar answered Nov 07 '22 00:11

Jorge Ramos