我第一次做添加觀察者調用不可靠的動作removeObserver:iPhone視頻播放
然後在通知函數中刪除觀察者。
我確定removerObserver被調用,因爲我在棧上看到它 但是,應用程序崩潰,好像在通知中心遺留了錯誤的內存引用。
我認爲有2個possiblities
我打了一個蘋果的bug
我調用的順序是錯誤的
這裏是調用playVideo函數我的代碼
//Initialize a MPMoviePlayerController object with the movie.
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector: @selector(movieReadyPlayMovieNow:)
name:MPMoviePlayerContentPreloadDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector: @selector(moviePlayBackFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
這裏是通知時,影片完成預加載
- (void) movieReadyPlayMovieNow:(NSNotification*)notification {
@try {
if(moviePlayer != nil){
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerContentPreloadDidFinishNotification
object:moviePlayer];
[moviePlayer play];
}
}
catch(id exception) {
NSLog(@"Error playing.");
}
}