我有MPMoviePlayerController的问题[iPhone SDK]

时间:2010-01-13 15:58:04

标签: iphone sdk

我想创建一个电影介绍的应用程序,就像gameloft游戏一样。因此,当应用程序已经发布时,电影播放正常,但在移动播放之前..我的FirstViewController xib文件首先显示然后电影开始播放!为什么?这是我的代码:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
NSBundle *bundle = [NSBundle mainBundle];
    NSString *moviePath = [bundle pathForResource:@"movie" ofType:@"m4v"];

    NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];

    MPMoviePlayerController *IntroMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];

    IntroMovie.movieControlMode = MPMovieControlModeHidden;
    [IntroMovie play];
}

2 个答案:

答案 0 :(得分:6)

在将第一个视图作为子视图添加到窗口之前,您需要等待电影完成播放。

这应该可以解决问题。将您的代码更改为:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *moviePath = [bundle pathForResource:@"Hafez-2" ofType:@"mov"];
    NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];


    MPMoviePlayerController *IntroMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    [IntroMovie setOrientation:UIDeviceOrientationPortrait animated:NO];
    [IntroMovie play];

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(moviePlaybackDidFinish:) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:nil]; 
}

- (void) moviePlaybackDidFinish:(NSNotification*)notification
{
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}

答案 1 :(得分:3)

另一种方法是添加不同的视图(例如简单的黑色背景),然后在视频播放完毕后替换它:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *moviePath = [bundle pathForResource:@"Hafez-2" ofType:@"mov"];
    NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];


    MPMoviePlayerController *IntroMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    [IntroMovie setOrientation:UIDeviceOrientationPortrait animated:NO];
    [IntroMovie play];

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(moviePlaybackDidFinish:) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:nil]; 
    // Create an initial pure black view
    UIView* blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    blackView.backgroundColor = [UIColor blackColor];
    [window addSubview:blackView]; // sends [blackView retain]
    [blackView release];
    [window makeKeyAndVisible];
}

- (void) moviePlaybackDidFinish:(NSNotification*)notification
{
    UIView* blackView = [[window subviews] objectAtIndex:0];
    [blackView removeFromSuperview]; // sends [blackView release]
    [window addSubview:viewController.view];
}

如果您需要从示例中修改源代码,请告诉我,我可以将其发布到某个地方