2011-04-18 46 views
1

我有以下代碼:addInput方法不返回

BOOL success; 

QTCaptureSession *session = [[QTCaptureSession alloc] init]; 
QTCaptureDevice *device = [QTCaptureDevice defaultInputDeviceWithMediaType: QTMediaTypeVideo]; 
success = [device open: &e]; 
if (!success) 
{ 
    NSLog(@"error opening input device: %@", e); 
    return; 
} 

QTCaptureDeviceInput *input = [QTCaptureDeviceInput deviceInputWithDevice: device]; 
success = [session addInput: input error: &e]; 
if (!success) 
{ 
    NSLog(@"error adding input device to session: %@", e); 
    return; 
} 

QTCaptureDecompressedVideoOutput *output = [[QTCaptureDecompressedVideoOutput alloc] init]; 
[output setDelegate: self]; 
success = [session addOutput: output error: &e]; 
if (!success) 
{ 
    NSLog(@"error adding output device to session: %@", e); 
    return; 
} 

[session startRunning]; 

這個位於運行時加載的束,並且是爲NSThread(又名的選擇器的方法的一部分它運行在在後臺線程中而不是主線程)。

我的問題是,調用#addInput:error:永遠不會返回。我在這裏錯過了什麼?

回答

0

問題是,該應用程序的主線程未使用通常由可可應用程序中的NSApplicationMain()提供的運行循環。相反,我在我自己的while循環內處理事件。

溶液呼籲:

CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, YES); 

Apple的QuickTime郵件列表上This thread對這個問題的更詳細的解釋。

+0

你能詳細解答這個問題嗎?我現在有同樣的問題,不知道如何解決這個與CFRunLoop ... – 2012-03-30 07:08:06

+0

IIRC我'撒'調用CFRunLoopRunInMode(kCFRunLoopDefaultMode,0,是);在我的while循環中的主線程。 – 2012-04-02 08:47:47