3

I want to play my video just like youtube in iOS using objective C and video file come from a URL. Can anyone guide me how to do this or there is any way to do video buffering in an efficient way.

Usman Javed
  • 2,437
  • 1
  • 17
  • 26

1 Answers1

1

Buffering like you tube can be accomplished with native Objective C "MPMovieController". You can check out this if it works for you.

    MPMoviePlayerViewController* MPmoviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlaybackComplete:)
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlaybackComplete:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlaybackComplete:)
                                                 name:MPMoviePlayerWillExitFullscreenNotification
                                               object:nil];
    moviePlayerController.moviePlayer.fullscreen=YES;
    moviePlayerController.moviePlayer.shouldAutoplay=YES;
    [self presentMoviePlayerViewControllerAnimated:moviePlayerController];

Declare MPmoviePlayerController a global variable.Then you handle notifications by defining its selector methods as per your requirements.

- (void)moviePlaybackComplete:(NSNotification *)notification
{
if([notification.name isEqual:MPMoviePlayerPlaybackDidFinishNotification])
{
    NSError *error = [[notification userInfo] objectForKey:@"error"];
    if (error)
    {
        NSLog(@"Did finish with error: %@", error);
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:nil];
    [moviePlayerController.moviePlayer stop];
    moviePlayerController = nil;
    [self dismissMoviePlayerViewControllerAnimated];
}
else if([notification.name isEqual:MPMoviePlayerWillExitFullscreenNotification])
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerWillExitFullscreenNotification
                                                  object:nil];
    [moviePlayerController.moviePlayer stop];
    moviePlayerController = nil;
    [self dismissMoviePlayerViewControllerAnimated];
}
}
CODE
  • 321
  • 2
  • 4
  • Your code works but it stuck in loading after opening video player. – Usman Javed May 14 '15 at 07:36
  • This is the default behaviour. First it buffers for few seconds depending on the network speed and then it autoplay. Same thing is on you tube. it first buffers the contents and then play it. – CODE May 15 '15 at 08:34