【问题标题】:Play video from url in objective c with buffering使用缓冲从目标 c 中的 url 播放视频
【发布时间】:2015-05-13 13:12:54
【问题描述】:

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

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

}

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

【问题讨论】:

  • 什么是视频格式和分辨率?您能举出您使用的视频示例吗?
  • 我使用的是 .flv 格式,其 url 来自服务器端。
  • 我有类似的问题,它只发生在横向格式的 1080 视频中。我想检查你的视频格式是什么。可以发个视频链接或者附上吗?

标签: ios objective-c iphone


【解决方案1】:

代码中没有任何内容表明延迟是发出请求和充分缓冲所需的时间。改进 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;
}

【讨论】:

  • 此代码不起作用。它打开视频播放器并卡在加载中。我已经尝试过这种代码。
  • 代码在已发布的应用程序中运行。考虑 - 作为调试步骤 - 使用应用程序包中的视频文件运行。或较小的远程文件。另外,考虑订阅 loadStateDidChange 通知,您可以在那里查看进度(并捕获错误)。
  • 刚刚注意到文件扩展名。 iPhone 不播放闪光灯。您将不得不使用 ffmpeg 或类似的东西重新编码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-31
  • 1970-01-01
  • 2017-01-15
  • 2018-11-06
相关资源
最近更新 更多