开发者

Will NSNotificationCenter call back method be called in main thread or background thread?

开发者 https://www.devze.com 2023-04-08 23:13 出处:网络
My code is just play a downloaded mp4 files and register the view controller to observe the notification of end of player.

My code is just play a downloaded mp4 files and register the view controller to observe the notification of end of player.

It works pretty good in not only iOS5 but also iOS4.

But I just want to know for sure that whether the call back method by NotificationCenter will be called in background thread or main thread. (loadMoviePlayerStateChanged:(NSNotification*)notification is call back method in my code) Do anyone know exactly about this?

- (void) playMovie:(NSURL *)fileURL {
 MPMoviePlayerViewController *MPVC = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];

 self.mMPVC = MPVC;
 self.mMPVC.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadMoviePlayerStateChanged:) 
                                              name:MPMoviePlayerLoadStateDidChangeNotification 
                                            object:self.mMPVC.moviePlayer];

 [MPVC.moviePlayer prepareToPlay];
 [MPVC release];
}

- (void) loadMoviePlayerStateChanged:(NSNotification*)notification {
int loadState = self.mMPVC.moviePlayer.loadState;
if(loadState & MPMovieLoadStateUnknown) {
    IGLog(@"The load state is not known at this time.");
    return;
} 

[[NSNotificationCenter defaultCenter] removeObserver:self 
                                                name:MPMoviePlayerLoadStateDidChangeNotification 
                                              object:self.mMPVC.moviePlayer];

[self.mMPVC.view setFrame:self.view.bounds];

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(moviePlayBackDidFinish:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification  
                                           object:self.mMPVC.moviePlayer];

/* if I do not use performSelectorOnMainThread method to add subview to UIViewController`s view, 
t开发者_StackOverflow社区he view of MPMoviePlayerViewController would not be removed from superview normally */

[self.view performSelectorOnMainThread:@selector(addSubview:) 
                            withObject:self.mMPVC.view 
                         waitUntilDone:YES];
[self.mMPVC.moviePlayer play];
}

- (void) moviePlayBackDidFinish:(NSNotification*)notification {
[self.mMPVC.moviePlayer stop];
[self.mMPVC.moviePlayer.view removeFromSuperview];

NSString* dstFilePath = [[_mPopupVC.mSelectedMovie decryptionFilePath] stringByAppendingPathExtension:@"mp4"];
[[NSFileManager defaultManager] removeItemAtPath:dstFilePath error:nil];

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


I got an answer for my question in apple developer site, the answer is,

"notifications safely thread themselves and perform their selectors on the main thread. however, your problem is that you should not be adding and removing the player view like that, use the presentModalVideo methods provided as a category in your view controller class."

and I solved the problem that i had. code is below..

- (void) playMovie
{
 /*
  *create and initialize MPMoviePlayerViewController with specified url and retain it
  */
 MPMoviePlayerViewController *MPVC = [[MPMoviePlayerViewController alloc] initWithContentURL:vodWebURL];
 self.mMPVC = MPVC;
 [MPVC release];
 self.mMPVC.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
 self.mMPVC.moviePlayer.shouldAutoplay = NO;
 [self.mMPVC.moviePlayer prepareToPlay];

  /*
   *register movie player to NSNotificationCenter
   */
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadMoviePlayerStateChanged:) 
                                              name:MPMoviePlayerLoadStateDidChangeNotification 
                                            object:self.mMPVC.moviePlayer];    
}


- (void) loadMoviePlayerStateChanged:(NSNotification*)notification 
{
 /*
  *do your work for the state of the movie player
  */
 int loadState = self.mMPVC.moviePlayer.loadState;
 if(loadState & MPMovieLoadStateUnknown) {
    NSLog(@"The load state is not known at this time.");
    return;
 } else if(loadState & MPMovieLoadStatePlayable) {
    NSLog(@"MPMovieLoadStatePlayable : The buffer has enough data that playback can begin, but it may run out of data before playback finishes.");
 } else if(loadState & MPMovieLoadStatePlaythroughOK) {
    NSLog(@"MPMovieLoadStatePlaythroughOK : Enough data has been buffered for playback to continue uninterrupted.");
 } else if(loadState & MPMovieLoadStateStalled) {
    NSLog(@"MPMovieLoadStateStalled : The buffering of data has stalled.");
 }

 /*
  *set frame of the view of MPMoviePlayerViewController and add it
  *call play method
  */
 [self.mMPVC.view setFrame:self.view.superview.bounds];
 [self.view.superview addSubview:self.mMPVC.view];
 [self.mMPVC.moviePlayer play];

 [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                 name:MPMoviePlayerLoadStateDidChangeNotification 
                                               object:self.mMPVC.moviePlayer];

 [[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(moviePlayBackDidFinish:)
                                              name:MPMoviePlayerPlaybackDidFinishNotification  
                                            object:self.mMPVC.moviePlayer];
}

- (void) moviePlayBackDidFinish:(NSNotification*)notification 
{
  /*
   *remove the view of MPMoviePlayerViewController
   *release MPMoviePlayerViewController
   */
 [self.mMPVC.moviePlayer stop];
 [self.mMPVC.moviePlayer.view removeFromSuperview];
 self.mMPVC = nil;

 [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                name:MPMoviePlayerPlaybackDidFinishNotification 
                                              object:self.mMPVC.moviePlayer];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号