0

我正在尝试使用 MPMoviePlayerController 显示视频,但无法使其正常工作。我的视频在我的服务器上。所以我试图以这种方式显示它:

-(void) playMovieAtURL: (NSURL*) theURL {
    moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theURL];
    moviePlayer.view.frame = self.view.bounds;
    moviePlayer.controlStyle = MPMovieControlModeDefault;
    moviePlayer.movieSourceType = MPMovieSourceTypeFile;
    moviePlayer.fullscreen = YES;

    [self.view addSubview:moviePlayer.view];
    [moviePlayer prepareToPlay];
    [moviePlayer play];
}

moviePlayer 是我的 viewController 的一个属性。给出的 URL 是正确的(在 safari 中测试)。我的视频是 .mov 视频。

当我的函数被调用时,我只是有一个黑屏。控件没有出现,我认为视频没有加载,因为在应用程序的状态栏中,没有显示网络活动指示器。

欢迎任何帮助。

编辑

我刚刚注意到,尝试从设备的 Safari 应用程序访问视频会导致以下结果:在此处输入图像描述

编辑 2

我的视频是 480 × 850,而 MPMoviePlayerController 只支持最大 640 × 480 的视频。所以不能用这个框架播放。我是否必须调整我的视频大小,或者是否有其他框架可以用来显示它?我只需要一个非常简单的播放器...

4

2 回答 2

1

这样做:

-(void)playMovieAtURL:: (NSURL*) url
{

    moviePlayer =  [[MPMoviePlayerController alloc]
                 initWithContentURL:url];

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

    moviePlayer.controlStyle = MPMovieControlStyleDefault;
    moviePlayer.shouldAutoplay = YES;
    [self.view addSubview:moviePlayer.view];
    [moviePlayer setFullscreen:YES animated:YES];
    [moviePlayer play];
    [self.view bringSubviewToFront:moviePlayer.view];
}


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

    if ([player
        respondsToSelector:@selector(setFullscreen:animated:)])
    {
        [player.view removeFromSuperview];
    }
}
于 2013-07-03T10:22:26.613 回答
1

// 你可以这样做

 -(void)playMovie:(id)sender
 {
   NSURL *url = [NSURL URLWithString:
  @"http://www.xxxx.com/movie.mov"];

_moviePlayer =  [[MPMoviePlayerController alloc]
             initWithContentURL:url];

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

_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES];

}

// 目标动作通知方法

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

if ([player
    respondsToSelector:@selector(setFullscreen:animated:)])
{
    [player.view removeFromSuperview];
}
}
于 2013-07-03T10:29:49.043 回答