【问题标题】:MPMoviePlayerController not working in iOS 5 xcode (with storyboard), but works perfectly in iOS 4MPMoviePlayerController 在 iOS 5 xcode(带有情节提要)中不起作用,但在 iOS 4 中完美运行
【发布时间】:2011-09-13 19:26:41
【问题描述】:

我在使用 iOS 5 测试版附带的新 xcode 播放电影时遇到问题。我创建了一个简单的项目(包含故事板和所有内容),并将此代码添加到按钮:

MPMoviePlayerController *moviePlayer;
NSString *path = [[NSBundle mainBundle] pathForResource:@"position" ofType:@"m4v"];
NSURL *videoURL = [NSURL fileURLWithPath:path];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
//[moviePlayer setControlStyle:MPMovieControlStyleDefault];
[moviePlayer.view setFrame: self.view.bounds];  // player's frame must match parent's
[self.view addSubview: moviePlayer.view];
[moviePlayer prepareToPlay];
[moviePlayer play];

令人困惑的是,当我将代码放入使用以前版本的 xcode(特别是 iOS 4.3 附带的那个)构建的项目中时,它可以完美运行。

有什么想法吗?

【问题讨论】:

  • iOS 5 处于保密协议下。请在Apple Developer Forums 讨论这个问题。
  • MPMoviePlayer && iOS 5.0 也有问题

标签: iphone objective-c ios ios5


【解决方案1】:

Objective-C 的新手,但我会试一试。使用 Xcode 4.2(为 iOS5 提供代码),默认情况下,新项目会启用 ARC(自动引用计数)。对于您提供的代码,由于您在此代码块中声明了moviePlayer,因此当moviePlayer 退出该代码块时会自动释放。在较旧的项目中,moviePlayer 会继续存在,可能会造成内存泄漏。我通过在类的头文件中声明moviePlayer来启用默认的Xcode 4.2 ARC设置,这意味着它只有在该类的对象实例被释放时才会被释放。

【讨论】:

  • 这不是真的。 ARC 是编译器设置,而不是 iOS 设置。 iOS 4 “支持” ARC,因为 ARC 会分析您的代码并在编译时根据需要简单地添加发布语句。它与操作系统本身无关。
  • 我的错,我的意思是默认的 xcode 设置,而不是 iOS 设置,我现在会更正它。
  • 感谢您的提示!我在使用 ARC 的项目中遇到了同样的问题,并将 MPMoviePlayerController 的声明移动到标题为我修复了它。
  • Ben 你能说清楚吗,“将 MPMoviePlayerController 的声明移动到标题为我修复了它”
  • 这解决了我的问题。我的视频在 ARC 之前播放,但在我重构为 ARC 后就无法播放。我所要做的就是在类头中声明 MoviePlayer,而不是在我的方法中本地声明。谢谢。
【解决方案2】:
MPMoviePlayerController *moviePlayer;

把它放在头文件(.h)和它的工作

【讨论】:

    【解决方案3】:

    我在这段代码中找到了解决方案:

    movieView = [moviePlayer view];
    
    [movieView setFrame: CGRectMake(0, 0, 1024, 768)];
    

    【讨论】:

      【解决方案4】:

      试试这个..它适用于 iOS 5。您可以编辑代码以使用 IBAction 按钮播放。祝你好运

      #import <UIKit/UIKit.h>
      #import <MediaPlayer/MediaPlayer.h>
      @interface Playing_Video_FilesViewController : UIViewController
      @property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
      @property (nonatomic, strong) UIButton *playButton;
      @end
       - (void) startPlayingVideo:(id)paramSender{
        NSBundle *mainBundle = [NSBundle mainBundle];
        NSString *urlAsString = [mainBundle pathForResource:@"Sample"
                                                     ofType:@"m4v"];
        NSURL    *url = [NSURL fileURLWithPath:urlAsString];
        if (self.moviePlayer != nil){
          [self stopPlayingVideo:nil];
      }
      
        self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
        if (self.moviePlayer != nil){
          [[NSNotificationCenter defaultCenter]
           addObserver:self
           selector:@selector(videoHasFinishedPlaying:)
           name:MPMoviePlayerPlaybackDidFinishNotification
           object:self.moviePlayer];
          NSLog(@"Successfully instantiated the movie player.");
      
          self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
      
          [self.moviePlayer play];
          [self.view addSubview:self.moviePlayer.view];
          [self.moviePlayer setFullscreen:YES
                                 animated:YES];
        } else {
          NSLog(@"Failed to instantiate the movie player.");
      } 
      }
      - (void) stopPlayingVideo:(id)paramSender {
        if (self.moviePlayer != nil){
          [[NSNotificationCenter defaultCenter]
           removeObserver:self
           name:MPMoviePlayerPlaybackDidFinishNotification
           object:self.moviePlayer];
          [self.moviePlayer stop];
          if ([self.moviePlayer.view.superview isEqual:self.view]){
            [self.moviePlayer.view removeFromSuperview];
      } }
      }
      - (void) viewDidUnload{
        self.playButton = nil;
        [self stopPlayingVideo:nil];
        self.moviePlayer = nil;
        [super viewDidUnload]; 
      }
      - (void) videoHasFinishedPlaying:(NSNotification *)paramNotification{
        /* Find out what the reason was for the player to stop */
        NSNumber *reason =
        [paramNotification.userInfo
         valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
        if (reason != nil){
          NSInteger reasonAsInteger = [reason integerValue];
          switch (reasonAsInteger){
            case MPMovieFinishReasonPlaybackEnded:{
              /* The movie ended normally */
      break; }
            case MPMovieFinishReasonPlaybackError:{
              /* An error happened and the movie ended */
              break; 
      }
      case MPMovieFinishReasonUserExited:{
              /* The user exited the player */
              break;
      } 
      }
          NSLog(@"Finish Reason = %ld", (long)reasonAsInteger);
          [self stopPlayingVideo:nil];
        } /* if (reason != nil){ */ 
      } 
      

      【讨论】:

        【解决方案5】:
        // link the method to a button and it will work in iOS5
        -(void)playMovie
        
            {
           NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] 
                         pathForResource:@"MOVIENAME" ofType:@"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];
        }
        

        【讨论】:

          【解决方案6】:

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-05-20
            • 2013-10-10
            • 1970-01-01
            • 2011-12-26
            • 2011-12-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多