MPMoviePlayerController无法在iOS 5 xcode(带故事板)中工作,但在iOS 4中运行良好

我在使用iOS 5 beta附带的新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附带的版本)构建的项目时,它可以正常工作。

有任何想法吗?

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

 MPMoviePlayerController *moviePlayer; 

把它放在标题(.h)文件及其工作上

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

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

试试这个..它可以在iOS 5中运行。您可以编辑代码以使其与IBAction按钮一起播放。 祝你好运

 #import  #import  @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){ */ } 
 // 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]; } 

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

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