2010-06-05 70 views
0

我的目標是發佈一個電影播放器​​(theMovie),然後在它完全發佈後開始另一個操作(所謂的playButtonClicked)。我使用了performSelector來延遲「playButtonClicked」1秒,並且效果很好。代碼是:如何知道電影放映人完成發佈?

[theMovie release]; [self performSelector:@selector(playButtonClicked)withObject:nil afterDelay:1];

但是,我不想總是延遲1秒。我想在「theMovie」完全發佈後立即啓動「playButtonClicked」。我嘗試了下面的代碼,但它不起作用,因爲[timer userInfo]永遠不會是零。 有誰知道如何檢查一個電影放映完成。

[theMovie release]; 
//[self performSelector:@selector(playButtonClicked) withObject:nil afterDelay:1]; 

NSTimer *atimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self 
                selector:@selector(waitForReleaseFinish:) 
                userInfo: (MPMoviePlayerController*) theMovie repeats:YES]; 

waitForRleaseFinish的代碼:(*的NSTimer)定時器:

if ([timer userInfo]==nil) //here I actually want to test if (theMovie==nil),but I don't know how to do and I'm not sure if it is a correct way to determine the release finished. 
{ 
    [timer invalidate]; 
    [self playButtonClicked]; 
} 

期待幫助。謝謝。

回答

1

沒有必要。

如果你只是鬆開球員,然後調用playButtonClicked,像這樣:

[theMovie release]; 
[self playButtonClicked]; 

,直到第一個結束,或直至theMovie釋放它不會執行第二行。這些都在同一個線程中,所以它會按順序執行。你不需要計時器。雖然在你等待完成的任務在新線程上執行的情況下,你可以使用回調,而不是猜測需要多長時間(這比1秒鐘少得多!)。另外,只要你不要誤解,「完全釋放」就是減去retainCount一個。它會在到達零時自動釋放。

正如一個側面說明,爲什麼在執行playButtonClicked之前釋放(取消分配?)Movie是重要的?

此外,您的waitForReleaseFinish:代碼工作,但它是沒有必要的,因爲電影將在定時器創建之前發佈。

+0

謝謝您的回覆。這聽起來有道理。不幸的是,如果我不拖延1-s(甚至0.5s),程序就無法正常工作。電影版本位於「moviePlayerDidFinsh」內,然後開始播放另一部電影。自從{}以來,移動再次被重新創建,它必須首先被釋放。 MPMoviePlayerController * mp = [[MPMoviePlayerController alloc] initWithContentURL:[self getMovieURL]]; \t如果(MP) \t { \t \t //保存電影選手對象 \t \t self.theMovie =熔點; \t \t [mp釋放]; \t} – vicky 2010-06-05 08:15:40

+0

如果電影是保留屬性,它應該自行釋放舊值。所以如果電影是一個保留屬性(它應該是),那麼你不應該手動發佈它。只需調用[self playButtonClicked]並將屬性設置爲新電影播放器​​時,它將釋放舊電影播放器​​。 – mk12 2010-06-05 16:36:43