2011-04-02 62 views
2

嘿,我用MPMoviePlayerViewController來顯示視頻。我不知道如何處理網絡問題。我想解僱錯誤的MPMoviePlayerViewController控制器。 dismissMoviePlayerViewControllerAnimated方法僅在第一次運行,第二次出現黑屏。iphone MPMoviePlayerViewController處理網絡問題(黑屏)

示例代碼:

// VideoViewController.h 
#import <MediaPlayer/MediaPlayer.h> 
@interface VideoViewController : MPMoviePlayerViewController 
{ 
} 
@end 

// VideoViewController.m 
@implementation VideoViewController 
- (void)viewDidLoad 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieDidLoad:) name:MPMoviePlayerContentPreloadDidFinishNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieDidLoad:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; 
} 

-(void)movieDidLoad:(NSNotification*)notification 
{ 
    [self dismissMoviePlayerViewControllerAnimated]; 
} 
@end 

// XController's function to call it 
- (void)showVideoView 
{ 
    VideoViewController * controller = [[VideoViewController alloc] initWithContentURL:[NSURL URLWithString:@"http://myvideos.com/movie.m4v"]]; 
    [self presentMoviePlayerViewControllerAnimated:controller]; 
    [controller.moviePlayer play]; 
    [controller release]; 
} 

請告訴我如何處理網絡問題。另請注意,該視頻始終處於全屏狀態。

+0

dismissMoviePlayerViewControllerAnimated不諾斯隱藏錯誤的看法。 – xpepermint 2011-04-02 16:13:10

回答

2

爲什麼你創建VideoViewController有什麼特別的原因?如果你想自定義某些東西,那麼你可以做所有的事情而不需要創建它。其他的事情是,這兩個通知你已經註冊了「movieDidLoad」這個方法,這將消除你的觀點。當視頻準備播放時,你的視圖將被解僱,因爲你註冊了「MPMoviePlayerContentPreloadDidFinishNotification」這個方法。此鏈接將幫助您更多:

- (void)playbackFinished:(NSNotification*)notification { 
NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]; 
switch ([reason intValue]) { 
    case MPMovieFinishReasonPlaybackEnded: 
     NSLog(@"playbackFinished. Reason: Playback Ended");   
     break; 
    case MPMovieFinishReasonPlaybackError: 
     NSLog(@"playbackFinished. Reason: Playback Error"); 
     break; 
     case MPMovieFinishReasonUserExited: 
     NSLog(@"playbackFinished. Reason: User Exited"); 
     break; 
    default: 
     break; 
} 
[self.movieController setFullscreen:NO animated:YES]; 

}

+0

是的...我也重寫shouldAutorotateToInterfaceOrientation方法。 – xpepermint 2011-04-02 13:16:01

+0

通知類「userinfo」有一個屬性。它會返回你的Nsdictionary。這本字典包含兩個重要的事情,一個是錯誤代碼,另一個是playfinish reason.from錯誤代碼,你可以得到失敗的確切原因。你可以讓你的應用程序更容易出錯。我現在沒有電腦,所以不能確切說你必須調試。 – Iducool 2011-04-02 13:41:22

+0

如果我不執行[[通知對象]停止],[self dismissMoviePlayerViewControllerAnimated]不工作,但調用停止,這意味着MPMovieFinishReasonPlaybackError被調用兩次。 – xpepermint 2011-04-02 14:25:42