NSNotification Does Not Notify
I have an application that sh开发者_JS百科ows a lot of videos. To load and play the file, I use the following code:
- (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];
}
}
Actually the system seems to works, but I have to push the button linked to "playVideooooo" two times, to let the notification work. If I move the [moviePlayerController play]; into the IBActions the video starts correctly. How should I get the Notifications work?
Question partially solved: the matter is not in the NSNotification but in PrepareToPlay.
精彩评论