MPMoviePlayerController给我一个黑色的空视图,没有video播放

iOS 5.1,这里是代码:

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]]; [player prepareToPlay]; [player.view setFrame: self.view.bounds]; // player's frame must match parent's [self.view addSubview: player.view]; // ... [player play]; 

其实这跟苹果说的一样,但是当我点击button来使用这个function的时候,只有一个黑色的Rectangle呆在那里,没有video播放,没有声音发生,也没有暗恋,所以我想知道如何做这个工作

对不起,很晚回复。 解决scheme有点奇怪。 但是在面临同样的问题时,它帮助我继续前进。

MPMovieSourceTypeStreaming是你错过的东西。

 MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]]; [player prepareToPlay]; [player.view setFrame: self.view.bounds]; // player's frame must match parent's player.movieSourceType = MPMovieSourceTypeStreaming; [self.view addSubview: player.view]; // ... [player play]; 

你可能正在发送你的

 [player play] 

消息太快了。

使用以下通知观察

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

要监听loadState以更改为可播放。 要检查更新,请执行以下操作:

 - (void)playMovie:(NSNotification *)notification { MPMoviePlayerController *player = notification.object; if (player.loadState & MPMovieLoadStatePlayable) { NSLog(@"Movie is Ready to Play"); [player play]; } } 

一旦准备就绪,电影应该开始播放。

尝试下面的代码从您的项目播放video资源:

  NSBundle *bundle = [NSBundle mainBundle]; NSString *moviePath = [bundle pathForResource:@"map" ofType:@"mp4"]; NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain]; MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; [player prepareToPlay]; [player.view setFrame: self.view.bounds]; // player's frame must match parent's [self.view addSubview: player.view]; // ... [player play]; 

谢谢..!

基于@ Dinesh的回答,你的URL创build是不正确的。 的价值

 [NSURL URLWithString:@"map.mp4"] 

将是零,因为@“map.mp4”不是一个有效的URL。 要从应用程序包创build一个有效的url,请按照Dinesh的说法。 要创build一个远程URL,你可以做类似的事情

 [NSURL URLWithString:@"http://www.jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v"] 

URL应该包含所有的部分:协议,主机,path,文件。

干杯,费利佩

在主窗口中添加您的电影播放器​​控制器视图,如:

  MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]]; [player prepareToPlay]; [player.view setFrame: self.view.bounds]; AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; [appDelegate.window addSubview: player.view]; [player play]; 

希望所以它会工作!

你的播放器应该是你的视图控制器的属性,如下所示:

。H:

 @property(nonatomic,weak)MPMoviePlayerController *moviePlayer; 

.M:

  MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]]; [player prepareToPlay]; [player.view setFrame: self.view.bounds]; // player's frame must match parent's //set the player to your property self.moviePlayer=player; [self.view addSubview: self.moviePlayer.view]; // ... [self.moviePlayer play]; 

请使用MPMoviePlayerViewController由于MP4文件。 当你使用MOV然后工作完美!

 MPMoviePlayerViewController *moviePlayerViewController; -(void)PlayVideoController:(NSString*)videoUrl1 { NSURL *fileURL = [NSURL URLWithString:videoUrl1]; moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL]; // Register for the playback finished notification. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer]; //Present [self presentMoviePlayerViewControllerAnimated:moviePlayerViewController]; // Play the movie! moviePlayerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile; [moviePlayerViewController.moviePlayer play]; } -(void)myMovieFinishedCallback:(NSNotification*)aNotification { [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer]; [moviePlayerViewController release], moviePlayerViewController = nil; } 

您可能需要检查videourl的空白处。 以下代码适用于我。

 - (IBAction)btnPlayVideo:(id)sender { self.strVideoUrl = [self.strVideoUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *fileURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@", self.strVideoUrl]]; NSLog(@"Url to play = %@", self.strVideoUrl); self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlaybackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayerController]; [self.moviePlayerController.view setFrame:self.view.bounds]; [self.view addSubview:self.moviePlayerController.view]; self.moviePlayerController.shouldAutoplay = YES; self.moviePlayerController.fullscreen = YES; self.moviePlayerController.controlStyle=NO; [self.moviePlayerController prepareToPlay]; self.moviePlayerController.movieSourceType = MPMovieSourceTypeStreaming; [self.moviePlayerController play]; } - (void)moviePlaybackComplete:(NSNotification *)notification { self.moviePlayerController = [notification object]; [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayerController]; [self.moviePlayerController.view removeFromSuperview]; self.closeVideAdProp.hidden = NO; btnCloseAd.hidden=FALSE; }