0

我有一個視頻在發佈時自動播放。短片結束後,會顯示黑屏。我想解僱子視圖來顯示圖像或自動加載另一個控制器?如何解除MPMoviePlayer中的子視圖?

下面是我的代碼:

(void)viewDidLoad 

{ 

    { 
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] 
             pathForResource:@"cover" ofType:@"mp4"]]; 

    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] 
             initWithContentURL:url]; 



    player.movieSourceType = MPMovieSourceTypeFile; 

    [player setControlStyle:MPMovieControlStyleNone]; 

    player.view.frame = CGRectMake(0, 0, 768, 960); 

    [self.view addSubview:player.view]; 
    [player play]; 


    player = nil; 

} 

感謝任何help..i'm在這個新秀。

回答

0

幾周前我想到了很多這樣的事情。查看可用的通知。 http://developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMoviePlayerController_Class/Reference/Reference.html

添加這樣的事情viewDidLoad中:

// Remove the movie player view controller from the "playback did finish" notification observers 
[[NSNotificationCenter defaultCenter] removeObserver:_moviePlayer 
               name:MPMoviePlayerPlaybackDidFinishNotification 
               object:_moviePlayer]; 
// Register this class as an observer instead 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(movieFinishedCallback:) 
              name:MPMoviePlayerPlaybackDidFinishNotification 
              object:_moviePlayer]; 

現在你有一個方法,你可以重新添加視圖或縮略圖,或什麼:

- (void)movieFinishedCallback:(NSNotification*)aNotification 
{ 
    // Obtain the reason why the movie playback finished 
    NSNumber *finishReason = [aNotification userInfo][MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]; 

    if ([finishReason intValue] == MPMovieFinishReasonPlaybackEnded) { 

     [self.view addSubview:self.moviePlayer.view]; 
    } 

(此代碼被從我的項目中調整,可能需要思考以適應!)

MPMoviePlayerController有很多通知,當電影播放時會被觸發ng,暫停,停止,完成等。您可以將代碼添加到這些方法中,以更好地控制演示文稿。在我的情況下,花了大約一天的時間進行研究和黑客攻擊(也許還有半天的清理和調整),但我設法獲得了一個非常棒的播放/暫停透明按鈕,並帶有「播放圖標」圖像當暫停或停止時重疊,所有基於玩家狀態的加載或卸載。這是一個簡單的自定義播放器控件,完全符合我的需求。完全可行的是,從一個玩家狀態開始,得到你想要的,然後進入下一個狀態。