i have a problem with MPMoviePlayerController [iPhone SDK]

。_饼干妹妹 提交于 2019-12-23 20:37:47

问题


i want create an app with movie intro just like gameloft games . so when application has lunched , movie plays fine but before the move plays .. my FirstViewController xib file show first then movie start to play ! why ? here is my code :

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

回答1:


An alternative is to add a different view (e.g. simple black background) and then replace it when the video has finished playing:

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

Let me know if you need modified source from your example and I can post it somewhere




回答2:


You need to wait for the movie to finish playing before adding your first view as a subview to window.

This should do the trick. Change your code to:

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


来源:https://stackoverflow.com/questions/2058067/i-have-a-problem-with-mpmovieplayercontroller-iphone-sdk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!