Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Video like youtube in iOS 8

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.

like image 915
Usman Javed Avatar asked Jan 30 '26 09:01

Usman Javed


1 Answers

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];
}
}
like image 110
CODE Avatar answered Feb 02 '26 02:02

CODE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!