2009-12-08 39 views
0

我第一次做添加觀察者調用不可靠的動作removeObserver:iPhone視頻播放

然後在通知函數中刪除觀察者。

我確定removerObserver被調用,因爲我在棧上看到它 但是,應用程序崩潰,好像在通知中心遺留了錯誤的內存引用。

我認爲有2個possiblities

  1. 我打了一個蘋果的bug

  2. 我調用的順序是錯誤的

這裏是調用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."); 
    } 
} 

回答

0

你從name:MPMoviePlayerContentPreloadDidFinishNotification註冊的通知從任何(無)對象調用:

[NSNotificationCenter defaultCenter]的addObserver:自我選擇:@選擇器(movieReadyPlayMovieNow :)名稱:MPMoviePlayerContentPreloadDidFinishNotification object:nil];

但是您要從moviePlayer對象取消註冊。

[[NSNotificationCenter defaultCenter] removeObserver:自名:MPMoviePlayerContentPreloadDidFinishNotification 對象:moviePlayer];

嘗試要麼登記爲雙方的對象nil在兩個或moviePlayer,看看是否能解決問題。

0

在通知中運行時刪除通知可能是件壞事。

嘗試在單獨的方法中刪除通知,並使用performSelector:withObject:afterDelay使用零延遲或performSelectorOnMainThread:來調用該方法。其中任何一個都將完成當前的運行循環,然後在通知調用之外執行removeObserver方法。