MPMoviePlayer - 黑屏 - iOS 5 - 无视频播放

时间:2011-10-28 14:29:34

标签: ios media-player mpmovieplayercontroller

在iOS 4中,此代码可以播放电影:

-(IBAction)playMovie:(id)sender
{
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"testbild1" ofType:@"m4v"]];
    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    UIView *testview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];

    [testview addSubview:moviePlayer.view];
    [self.view addSubview:testview];
    //[self.view addSubview:moviePlayer.view];
    //[moviePlayer setFullscreen:YES animated:YES];
}  

- (void)moviePlaybackComplete:(NSNotification *)notification  
{  
    MPMoviePlayerController *moviePlayerController = [notification object];  
    [[NSNotificationCenter defaultCenter] removeObserver:self  
                                                    name:MPMoviePlayerPlaybackDidFinishNotification  
                                                  object:moviePlayerController];  

    [moviePlayerController.view removeFromSuperview];  
    //[moviePlayerController release];  
}  

现在,我只得到一个黑屏。没有控制,没有。视频路径是正确的,我测试了。如果我通过按钮单击添加一个白色的子视图,它可以工作。所以这个方法叫做。

提前致谢!

2 个答案:

答案 0 :(得分:13)

我已经通过输入.h文件解决了这个问题

MPMoviePlayerController *moviePlayer;

使用ARC的iOS 5与iOS 4不同。

.m文件必须是:

moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];

答案 1 :(得分:1)

如果您使用的是iOS 5.0或5.1,请检查您的moviePlyer是否有如下设置。

[moviePlayer setControlStyle:MPMovieControlStyleFullscreen];

如果是这样,当您设置[moviePlayer setFullScreen:YES animated:YES];时 然后将调用MPMoviePlayerPlaybackDidFinishNotification通知,而不是调用MPMoviePlayerWillExitFullscreenNotification或MPMoviePlayerDidExitFullscreenNotification