【发布时间】:2014-02-14 06:03:34
【问题描述】:
我在我的项目中使用MPMoviePlayer。我已经注册了电影播放器完成通知,它运行良好。每当收到有关电影播放器错误的通知时,我都会显示错误警报。但问题是错误警报显示多次。之所以会发生这种情况,是因为同一错误同时收到了多个通知。我曾尝试使用布尔变量来控制警报显示,但由于同时收到通知,所以它不起作用。我应该采用什么方法,请建议。
我的通知方法代码:
MPMovieFinishReason reason = [[[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
NSError *errorMsg = [[notification userInfo] valueForKey:@"error"];
NSString *errmsg = [errorMsg localizedDescription];
if (reason == 1 && !errorReceived){
NSError *errorMsg = [[notification userInfo] valueForKey:@"error"];
NSString *errmsg = [errorMsg localizedDescription];
[self showErrorAlert];
}
注册通知:
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(moviePlayerDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.player];
用于移除观察者,在viewWillDisappear
[[NSNotificationCenter defaultCenter]removeObserver:self];
【问题讨论】:
-
您是否确认您只注册了一次通知?您是否在适当的时候取消注册通知?
-
您是如何注册通知的?是否有可能多次注册此通知?您也有可能在通知触发后忘记从通知中删除一些观察者。
-
是的,我注册过一次,也注销过。
-
请出示注册码和注销码。
-
您在收到通知时尝试删除通知??
标签: ios iphone objective-c concurrency mpmovieplayer