iosobjective-ciphonempmovieplayercontrollermpmovieplayer

MPMoviePlayerController automatic pause at second X


I have to automatic stop the player at second X and I don't know which kind of notification I have to used. After, when user taps anywhere on the screen the player continue to run the video.

- (IBAction)playMovie:(id)sender {
    NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"m4v"];
    NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
    _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];

    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:_moviePlayer];

    //here I don't know which notification I have to used
    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(moviePlayBackPause:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:_moviePlayer];

    _moviePlayer.controlStyle = MPMovieControlStyleDefault;
    _moviePlayer.initialPlaybackTime = 2.5;

    _moviePlayer.shouldAutoplay = YES;
    [self.view addSubview:_moviePlayer.view];
    [_moviePlayer setFullscreen:YES animated:NO];
    [_moviePlayer play];
}

I tried to catch the movie frame and analyzed it to see if is time to pause the video.

- (void) moviePlayBackPause:(NSNotification*)notification{
    // check if it's time to pause the video 
    if([_moviePlayer currentPlaybackTime] == 6.0){
         [_moviePlayer pause];
    }
}
  1. Which type of notification I have to use to catch the current time of the video?

Thanks in advance for all your responses! Kind regards!


Solution

  • You can use a NSTimer to stop your video. Should exist a better way to do it, but with timer you can do it as well.

    [_moviePlayer play];
    [NSTimer scheduledTimerWithTimeInterval:6 //Time in seconds
                                     target:self
                                   selector:@selector(moviePlayBackPause) //Method called when the timer is completed.
                                   userInfo:nil
                                    repeats:NO];
        }
    }
    
    - (void) moviePlayBackPause {
      [_moviePlayer pause];
    }