2012-03-05 53 views
7

我正在創建MPMoviePlayerController對象並以全屏模式流式傳輸視頻。MPMoviePlayerController在單擊完成時不會刪除視圖

我使用一個UIViewController顯示短片視圖。

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    //http://www.youtube.com/watch?feature=player_detailpage&v=ebeQaznNcmE 
    NSURL *url = [NSURL URLWithString:@"http://a1408.g.akamai.net/5/1408/1388/2005110405/1a1a1ad948be278cff2d96046ad90768d848b41947aa1986/sample_mpeg4.mp4"]; 
    MPMoviePlayerController *mPlayer = [[MPMoviePlayerController alloc]initWithContentURL:url]; 
    mPlayer.view.frame = gMainView.frame; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(moviePlayBackDidFinish:) 
              name:MPMoviePlayerPlaybackDidFinishNotification 
              object:mPlayer]; 
    mPlayer.shouldAutoplay = YES; 
    mPlayer.controlStyle = MPMovieControlStyleFullscreen; 
    [gMainView addSubview:mPlayer.view]; 
    [mPlayer prepareToPlay]; 
    [mPlayer setFullscreen:YES animated:YES]; 
    [mPlayer play]; 
} 


- (void)moviePlayBackDidFinish:(NSNotification*)notification { 
    int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue]; 
    if (reason == MPMovieFinishReasonPlaybackEnded) { 
     //movie finished playing 
    } 
    else if (reason == MPMovieFinishReasonUserExited) { 
     //user hit the done button 
     MPMoviePlayerController *moviePlayer = [notification object]; 

     [[NSNotificationCenter defaultCenter] removeObserver:self  
                 name:MPMoviePlayerPlaybackDidFinishNotification 
                 object:moviePlayer]; 

     if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) { 
      [moviePlayer.view removeFromSuperview]; 
     } 
     [moviePlayer release]; 
    } 
    else if (reason == MPMovieFinishReasonPlaybackError) { 
     //error 
    } 
} 

在單擊完成,視頻視覺從屏幕中刪除,但在控制不從屏幕除去視圖不從屏幕中移除。

控件確實來「//用戶點擊完成按鈕」。它確實執行代碼以從超級視圖中刪除視圖,我通過添加日誌來檢查,但是控件不會從屏幕上移除,並且視圖也不會從屏幕中刪除。 我在做什麼錯?

編輯:

如果我使用MPMoviePlayerViewController那麼它甚至沒有等我按完成。視頻完成後,它會自動刪除視圖。但我不想那樣。

編輯:

如果刪除 「[MPLAYER setFullscreen:YES動畫:是]」 然後點擊完成時,該視圖被完全去除。但是視頻並沒有全屏顯示,狀態欄變成灰色,這又是我不想要的。

+0

你採取許多措施來形容你不想要什麼,但至少對我來說,這並還沒有真正說明你真正想要達到的目標。 – Till 2012-03-05 22:45:47

+0

控件不會從屏幕上移除,播放器視圖也不會從屏幕上移除。 – Anand 2012-03-06 05:53:22

+1

嘗試這種解決方案:http://stackoverflow.com/questions/6142571/mpmovieplayer-done-button-issue/6142685#6142685 – Till 2012-03-06 08:50:56

回答

10

下面的代碼爲我工作,希望它也可以幫助你。

-(IBAction)playVedio:(id)sender{ 
mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; 

    [[mp moviePlayer] prepareToPlay]; 
    [[mp moviePlayer] setUseApplicationAudioSession:NO]; 
    [[mp moviePlayer] setShouldAutoplay:YES]; 
    [[mp moviePlayer] setControlStyle:2]; 
    [[mp moviePlayer] setRepeatMode:MPMovieRepeatModeOne]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; 
    [self presentMoviePlayerViewControllerAnimated:mp]; 

} 

-(void)videoPlayBackDidFinish:(NSNotification*)notification { 

    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; 

    [mp.moviePlayer stop]; 
    mp = nil; 
    [mp release]; 
    [self dismissMoviePlayerViewControllerAnimated]; 
} 
+0

Sir Sirji還添加了一些更多的代碼,其他人可以很容易地理解你的答案,你做了什麼以及代碼問題是什麼問題你做了什麼來克服它,反正感謝解決方案: ) – MKJParekh 2012-03-06 09:29:55

+0

vl下次請注意abt .. !! :) – Sirji 2012-03-06 09:35:11

+4

你不應該誇大其詞。 – Till 2012-03-06 10:43:57

0
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    id presentedViewController = [window.rootViewController presentedViewController]; 
    NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil; 

    if (window && [className isEqualToString:@"AVFullScreenViewController"]) { 

     return UIInterfaceOrientationMaskAll; 

    } else { 

     UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation; 

     if(UIInterfaceOrientationIsPortrait(interfaceOrientation)) 

     { 

     } 

     else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)) 

     { 


     } 

     return UIInterfaceOrientationMaskPortrait; 

     CGRect frame = [UIScreen mainScreen].applicationFrame; 
     CGSize size = frame.size; 
     NSLog(@"%@", [NSString stringWithFormat:@"Rotation: %s [w=%f, h=%f]", 
         UIInterfaceOrientationIsPortrait(interfaceOrientation) ? "Portrait" : "Landscape", 
         size.width, size.height]); 
    } 
} 
相關問題