2010-01-13 42 views
3

我想創建一個應用程序與電影簡介就像gameloft遊戲。所以當應用程序已經發布時,電影播放良好,但在播放之前..我的FirstViewController xib文件首先顯示電影開始播放!爲什麼?這裏是我的代碼:我有一個問題,MPMoviePlayerController [iPhone SDK]

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

你的應用程序有什麼午餐? :) –

回答

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

讓我知道你是否需要修改後的源代碼,我可以將它發佈到某個地方

+0

:O:O:O:O 我真的厭倦了嘗試1000000種類型的代碼!仍然不會改變爲什麼爲什麼爲什麼爲什麼? 你能創建一個你的代碼示例項目嗎? 我不知道我能做什麼! – Momi

+1

我添加了我在此處發佈的原始源的修改版本http://www.filefactory.com/file/a2fbd87/n/MoviePlayer.zip – cidered

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

沒有任何改變!!!!!!!!!!!!!!!!!!!!!!!!!!! – Momi

+1

爲了澄清,您希望視頻在您的第一個視圖顯示之前播放?我下載了你的源代碼(從你現在刪除的答案中),這確實有效,所以你做錯了什麼!你確定你移動這些行開始[窗口... out of applicationDidFinishLaunching:並進入moviePlaybackDidFinish:??? – cidered

+0

是啊,我確定!但我不明白爲什麼不工作! 該帖子已被刪除 – Momi