4

我正在开发我的应用程序。它需要通过服务器在 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];
4

2 回答 2

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

希望对你有帮助..

于 2012-07-16T06:52:35.993 回答
2

据我所知,您有两种选择:

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

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

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

希望这可以帮助。

干杯!

于 2012-07-16T07:30:16.893 回答