【问题标题】:MPMoviePlayerViewController works, MPMoviePlayerController does not..why?MPMoviePlayerViewController 有效,MPMoviePlayerController 无效..为什么?
【发布时间】:2011-10-21 14:31:20
【问题描述】:

我有这个代码

NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"d" ofType:@"mp4"];  
NSURL    *url    =   [NSURL fileURLWithPath:filepath];  

//part 1
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: url];
[player.view setFrame: self.view.bounds];  
//[player prepareToPlay];
//[player setShouldAutoplay:YES];
//[player setControlStyle:2];
[self.view addSubview: player.view];
[player play];

//part2
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[mp moviePlayer] prepareToPlay];
[[mp moviePlayer] setShouldAutoplay:YES];
[[mp moviePlayer] setControlStyle:2];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:mp];
[self presentMoviePlayerViewControllerAnimated:mp];

第 2 部分有效,第 1 部分无效...他们使用相同的 url,我几乎从 ADC 站点复制并粘贴了 sn-p

我用的是ios5

【问题讨论】:

  • 感谢您提供第 2 部分。只看过第 1 部分的代码,它让我发疯,因为它不起作用。第 2 部分代码效果很好!

标签: iphone media-player ios5


【解决方案1】:

这个问题发布已经很久了,但答案是你必须保留MPMoviePlayerController变量的引用。

如果您在函数中有 part1 的代码,请在 .h 文件中声明 MPMoviePlayerController *player。如果您不这样做(并且您正在使用 ARC 进行开发),您的 player 变量将在您退出函数后立即被释放。请注意,您将player.view 添加到self.view,因此玩家的视图会被保留,但玩家的控制器不会并且会被释放。

所以你的 .h 文件中应该有:

MPMoviePlayerController *player;

在这种情况下,您的函数必须类似于:

-(void) playMovie
{
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] 
                                         pathForResource:@"mymovie" 
                                         ofType:@"mov"]];    

    player = [[MPMoviePlayerController alloc] initWithContentURL: url];
    [player.view setFrame: self.view.bounds];  
    [self.view addSubview: player.view];
    [player play];        
}

【讨论】:

    【解决方案2】:

    我在带有 MPPlayerController 的 iOS 5 上遇到了类似的问题,我检查了 Apple 的示例项目,不同之处只是设置了框架,所以我手动设置了框架并且效果很好。

        [[[self moviePlayer] view] setFrame:CGRectMake(0, 0, 320, 480)];
    

    【讨论】:

      【解决方案3】:

      在未加载视频之前,您无法将播放器视图添加到主视图。

      所以你应该替换这一行:

      [self.view addSubview: player.view];
      

      通过这个:

      [[NSNotificationCenter defaultCenter] 
          addObserver:self 
          selector:@selector(movieLoadStateDidChange:) 
          name:MPMoviePlayerLoadStateDidChangeNotification 
          object:player];
      

      并添加此方法:

      -(void)movieLoadStateDidChange: (NSNotification*)notification{
          if (player.loadState & MPMovieLoadStatePlayable == MPMovieLoadStatePlayable) {
              [[NSNotificationCenter defaultCenter] 
                  removeObserver:self 
                  name:MPMoviePlayerLoadStateDidChangeNotification 
                  object:player] ; 
              [self.view addSubview:player.view];
          }
      }
      

      【讨论】:

      • MPMovieLoadStatePlayable == MPMovieLoadStatePlayable ??这是什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 2020-05-14
      • 2020-10-07
      • 2020-03-09
      • 2023-03-30
      相关资源
      最近更新 更多