通过缓冲在目标c中播放来自URL的视频

时间:2015-05-13 13:12:55

标签: ios objective-c iphone

大家好我正在处理一个应用程序,其中我有一个视频网址,我必须从该网址播放视频。我已经从这段代码完成了这项工作

- (IBAction)btnPlayVideo:(id)sender
{
    NSString *fileName = @"Server Address/Vdieo.flv";

    NSURL *streamURL = [NSURL URLWithString:fileName];

    mPlayerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:streamURL];


    [self.view addSubview:mPlayerVC.view];

    //play movie

    MPMoviePlayerController *player = [mPlayerVC moviePlayer];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

    player.view.frame = self.view.frame;
    [player setFullscreen:YES animated:YES];
    [self.view addSubview:player.view];
    [player prepareToPlay];
    [player play];

}

// ============其他方法====================

- (void)willEnterFullscreen:(NSNotification*)notification {
    NSLog(@"willEnterFullscreen");
}

- (void)enteredFullscreen:(NSNotification*)notification {
    NSLog(@"enteredFullscreen");
}

- (void)willExitFullscreen:(NSNotification*)notification {
    NSLog(@"willExitFullscreen");
}

- (void)exitedFullscreen:(NSNotification*)notification {
    NSLog(@"exitedFullscreen");
    [mPlayerVC.view removeFromSuperview];
    mPlayerVC = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)playbackFinished:(NSNotification*)notification {
    NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
    switch ([reason intValue]) {
        case MPMovieFinishReasonPlaybackEnded:
            NSLog(@"playbackFinished. Reason: Playback Ended");
            break;
        case MPMovieFinishReasonPlaybackError:
            NSLog(@"playbackFinished. Reason: Playback Error");
            break;
        case MPMovieFinishReasonUserExited:
            NSLog(@"playbackFinished. Reason: User Exited");
            NSLog(@"exitedFullscreen");

            [[NSNotificationCenter defaultCenter] removeObserver:self];
            break;
        default:
            break;
    }

    [mPlayerVC.view removeFromSuperview];
    mPlayerVC = nil;

}

我的问题是,当此代码运行视频播放器打开并开始加载时,运行视频需要太多时间。任何人都可以指导我如何从互联网快速运行视频吗?

1 个答案:

答案 0 :(得分:0)

代码中没有任何内容表明延迟不是产生请求和充分缓冲所需的时间。改进UE的最常用技术是尽可能早地开始加载,甚至在用户请求回放之前。

如果可以,则应按如下方式重新组织代码:

// hang on to the movie player
@property(nonatomic,retain) MPMoviePlayerController *mp;

// call this as soon as its possible to know the user might want to see the video
- (void)primeVideo {
    NSString *fileName = @"Server Address/Vdieo.flv";
    NSURL *streamURL = [NSURL URLWithString:fileName];

    MPMoviePlayerController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:streamURL];

    // do this also in dealloc
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

    mp.shouldAutoplay = NO;
    [mp prepareToPlay];
    self.mp = mp;
} 

在不改变UI的情况下尽可能多地做好准备。来自按钮操作的其余代码保留,稍微调整一下......

- (IBAction)btnPlayVideo:(id)sender {

    // if there's any way that the user can request playback before
    // you've called primeVideo, check for that here.  But hopefully you
    // can call primeVideo before user even sees the play button
    if (!self.mp) [self primeVideo];

    self.mp.view.frame = self.view.frame;
    [self.mp setFullscreen:YES animated:YES];
    [self.view addSubview:self.mp.view];

    MPMovieLoadState state = [self.mp loadState];
    if (state & MPMovieLoadStatePlaythroughOK) [self.mp play];
    else self.mp.shouldAutoplay = YES;
}