的MPMoviePlayerController给了我黑色的空视图,没有视频播放

问题描述:

的iOS 5.1,这里是代码:的MPMoviePlayerController给了我黑色的空视图,没有视频播放

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]; 
其实

,这仅仅是一样的苹果基准说,但是当我点击按钮使用此功能,只有一个黑色的Rectangle呆在那里,没有视频播放,没有声音发生,没有暗恋,所以我想知道如何使这项工作

+0

我的代码工作与否? – Dinesh

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

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]; 

谢谢..!

+0

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'*** - [NSURL initFileURLWithPath:]:nil字符串参数'并感谢您的帮助,但我曾试过这种代码,这是控制台信息 –

+0

将您的视频添加到“复制束资源”。 –

建立在@ Dinesh的回答,您的URL创建不正确。值为

[NSURL URLWithString:@"map.mp4"] 

将为零,因为@“map.mp4”不是有效的URL。要从应用程序的包中创建有效的网址,请执行Dinesh的说法。要创建一个你不喜欢

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

的URL的东西应该包含所有远程URL它的部分:协议,主机,路径,文件。

干杯,菲利普

+0

谢谢,但实际上我想使用本地mp4文件,而不是流,[NSBundle mainBundle]似乎工作。 –

+1

我很高兴它为你工作。请考虑授予Dinesh的答案。这对你来说也是最好的,因为不授予它会伤害你的声誉。 – fsaint

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

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]; 
+0

这不是原来的海报问题,但它绝对是我的! – shulmey

+0

so @shulmey,有帮助吗? –

+4

我的问题是我没有设置movieSourceType属性。所以如果它帮助任何人。确保你设置了这个属性。默认情况下,它必须设置为MPMovieSourceTypeUnknown。当我把它放到MPMovieSourceTypeFile。它为我工作。 –

你的球员应该是你的视图控制器的属性,像这样:

.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; 
} 

你可能会过早地发送您的

[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]; 
    } 
} 

电影应该开始播放一旦它准备好了。

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

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]; 

希望如此,很有效!

您可能希望跨空格检查视频网址。以下代码适用于我。

- (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; 
}