2011-08-02 47 views
0

我創建了一個全局變量:無法釋放的MPMoviePlayerController

MPMoviePlayerController *player; 

我玩用下面的方法視頻:

- (IBAction爲)的playMovie:(的NSString *)videoName ViedeoType:(的NSString * )的VideoType {

ViewVideoSubview.alpha = 0; 

NSString *url = [[NSBundle mainBundle] 
       pathForResource:videoName 
       ofType:videoType]; 


player = 
[[MPMoviePlayerController alloc] 
initWithContentURL:[NSURL fileURLWithPath:url]]; 


player.shouldAutoplay =YES; 



[ViewVideoSubview addSubview:player.view]; 



[[NSNotificationCenter defaultCenter] 
addObserver:self 
selector:@selector(movieFinishedCallback:)             
name:MPMoviePlayerPlaybackDidFinishNotification 
object:player]; 

}

和當視頻結束playi ng本身就是所謂的以下方法:

- (void) movieFinishedCallback:(NSNotification*) aNotification { 



    [player.view removeFromSuperview]; //d1 
    MPMoviePlayerController *playerParam = [aNotification object]; 
    [[NSNotificationCenter defaultCenter] 
    removeObserver:self 
    name:MPMoviePlayerPlaybackDidFinishNotification 
    object:playerParam]; 

    [player release]; 

} 

一切工作到目前爲止很好。問題是我有一個按鈕,當按下我需要加載另一個視圖控制器。我能夠加載該視圖控制器,但視頻仍在後臺播放。我不明白爲什麼我釋放播放器時出現錯誤。我的臨時解決方案是停止視頻,然後加載其他視圖控制器,以便視頻不會在後臺播放。

我想到的另一個解決方案是在播放完1秒前播放視頻,以便使用movieFinishedCallback方法發佈視頻。我不知道如何能夠將視頻「快進」到那一點。我是新來的Objective-C,我不知道什麼是aNotification參數,否則我只是用適當的參數調用該方法。

讓我告訴你,我得到的錯誤:

enter image description here enter image description here

回答

2

我覺得你的問題就出在你正試圖在該方法中刪除觀察者movieFinishedCallback

來這裏的路上你傳遞一個指向你的全球房地產玩家的指針。

MPMoviePlayerController *playerParam = [aNotification object]; 

,在這裏你都invocing的方法去除對此對象playerParam

[[NSNotificationCenter defaultCenter] 
removeObserver:self 
name:MPMoviePlayerPlaybackDidFinishNotification 
object:playerParam]; 

現在你,因爲你要發送一個指針(playerParam)到您的player得到EXEC_BAD_ACCESS(已經爲所有通知觀察者發佈到某處)導致在一個不存在的對象上調用removeObserver的操作的方法(removeObserver)。

而不是使用

[[NSNotificationCenter defaultCenter] 
    removeObserver:self 
    name:MPMoviePlayerPlaybackDidFinishNotification 
    object:playerParam]; 

嘗試

[[NSNotificationCenter defaultCenter] 
    removeObserver:self 
    name:MPMoviePlayerPlaybackDidFinishNotification 
    object:nil]; 

使你的對象無將:

- (空)removeObserver:(ID)notificationObserver名稱:(NSString的 * )notificationName對象:(id)notificationSender

notificationSender ... 當無時,接收者不使用通知發送者作爲標準 進行刪除。

更多信息可以在NSNotificationCenter Class Reference

找到