在iOS 8中播放youtube之类的视频

时间:2020-12-31 18:57:16

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.

我想用objective C播放我的视频,就像youtube在iOS里一样,视频文件来自URL。有人能告诉我怎么做吗?或者有什么方法可以有效地进行视频缓存。

1 个解决方案

#1


1  

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

像tube这样的缓冲可以通过本机Objective C“MPMovieController”实现。如果对你有用,你可以看看这个。

    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.

将MPmoviePlayerController声明为全局变量。然后根据需要定义它的选择器方法来处理通知。

- (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];
}
}

#1


1  

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

像tube这样的缓冲可以通过本机Objective C“MPMovieController”实现。如果对你有用,你可以看看这个。

    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.

将MPmoviePlayerController声明为全局变量。然后根据需要定义它的选择器方法来处理通知。

- (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];
}
}