在IOS中通过HTTP流式传输视频

时间:2012-07-16 06:46:08

标签: iphone ios

我正在处理我的应用程序。它需要通过服务器在iPhone上播放视频。我有一个视频链接http://www.cwtmedia.se/cwtvideo.mp4。任何机构都可以建议我如何在MPMoviePlayerController上执行此操作。我正在使用此代码,但它不起作用。

enter code here


NSURL *url = [NSURL fileURLWithPath:@"http://www.cwtmedia.se/cwtvideo.mp4"];
moviePlayer1 = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer1.view];
moviePlayer1.view.frame = CGRectMake(0, 0, 320, 416); 
moviePlayer1.fullscreen=YES;
[moviePlayer1 setFullscreen:NO animated:YES];
moviePlayer1.controlStyle = MPMovieControlStyleFullscreen;

[moviePlayer1 play];

2 个答案:

答案 0 :(得分:3)

顺便说一句,这是我如何使用mpmovieplayercontroller进行流式传输:

NSURL *url = [NSURL URLWithString:videoUrl];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[moviePlayer setControlStyle:MPMovieControlStyleDefault];
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
CGRect frame;
if(self.interfaceOrientation ==UIInterfaceOrientationPortrait)
    frame = CGRectMake(20, 69, 280, 170);
else if(self.interfaceOrientation ==UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation ==UIInterfaceOrientationLandscapeRight)
    frame = CGRectMake(20, 61, 210, 170);
[moviePlayer.view setFrame:frame];  // player's frame must match parent's
[self.view addSubview: moviePlayer.view];
[self.view bringSubviewToFront:moviePlayer.view];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlayBackDidFinish:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:moviePlayer];

[moviePlayer prepareToPlay];
[moviePlayer play];

然后是委托方法:

- (void) moviePlayBackDidFinish:(NSNotification*)notification {
       MPMoviePlayerController *player = [notification object];
       [[NSNotificationCenter defaultCenter] 
            removeObserver:self
            name:MPMoviePlayerPlaybackDidFinishNotification
            object:player];

       if ([player respondsToSelector:@selector(setFullscreen:animated:)]){
          //self.navigationController.navigationBarHidden = YES;
          [player.view removeFromSuperview];
       }
}

希望这会对你有帮助..

答案 1 :(得分:2)

据我所知,你有两个选择:

1)首先下载文件并在本地播放。像这样:

   NSString *url = [[NSBundle mainBundle] pathForResource:@"cwtvideo" ofType:@"mp4"];
   MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];

2)使用HTTP流媒体协议。据我所知,HTTP streaming是MPMoviePlayerController已知的唯一流媒体协议。

希望这有帮助。

干杯!