NSNotification不通知

时间:2011-02-28 15:48:51

标签: ios4 nsnotificationcenter

我有一个显示很多视频的应用程序。要加载和播放文件,我使用以下代码:

- (IBAction)playVideoooo {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:
                         [NSURL URLWithString:@"/UnioneDiCentro2011_Live.isml/manifest(format=m3u8-aapl)"]];
switch ( [self interfaceOrientation] ) {
    case UIInterfaceOrientationPortrait:
    case UIInterfaceOrientationPortraitUpsideDown:
        [[moviePlayerController view] setFrame:CGRectMake(0, 0, P_WIDTH, P_HEIGHT)];
        break;
    case UIInterfaceOrientationLandscapeLeft:
    case UIInterfaceOrientationLandscapeRight:
        [[moviePlayerController view] setFrame:CGRectMake(0, 0, L_WIDTH, L_HEIGHT)];
        break;
}
[moviePlayerController prepareToPlay];
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(moviePlayerLoadStateChanged:) 
                                             name:MPMoviePlayerLoadStateDidChangeNotification 
                                           object:nil]; // Register that the load state changed (movie is ready)
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(moviePlayBackDidFinish:) 
                                             name:MPMoviePlayerPlaybackDidFinishNotification 
                                           object:nil];
[[self view] addSubview:[moviePlayerController view]];
}

- (void)moviePlayerLoadStateChanged:(NSNotification*)notification {
// Unless state is unknown, start playback
if ([moviePlayerController loadState] != MPMovieLoadStateUnknown) {
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:MPMoviePlayerLoadStateDidChangeNotification 
                                                  object:nil];
    [[UIApplication sharedApplication] setStatusBarOrientation:[self interfaceOrientation] 
                                                      animated:YES];
            [moviePlayerController play];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

}
}

- (void)moviePlayBackDidFinish:(NSNotification*)notification {    
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[NSNotificationCenter  defaultCenter] removeObserver:self 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:nil];
switch ( [self interfaceOrientation] ) {
    case UIInterfaceOrientationPortrait:
    case UIInterfaceOrientationPortraitUpsideDown:
        [[moviePlayerController view] setFrame:CGRectMake(0, 0, P_WIDTH, P_HEIGHT)];
        break;
    case UIInterfaceOrientationLandscapeLeft:
    case UIInterfaceOrientationLandscapeRight:
        [[moviePlayerController view] setFrame:CGRectMake(0, 0, L_WIDTH, L_HEIGHT)];
        break;
}       
if ( [moviePlayerController isFullscreen] ) {
    [moviePlayerController setFullscreen:NO];
}
}

实际上系统似乎有效,但我必须按两次链接到“playVideooooo”的按钮,让通知工作。如果我移动[moviePlayerController播放];在IBActions中,视频正确启动。我应该如何获得通知?

1 个答案:

答案 0 :(得分:0)

问题部分解决:问题不在NSNotification中,而是在PrepareToPlay中。