2014-01-28 155 views
1

我正在爲我的應用程序使用AUGraph,並且在停止Graph時遇到了一些麻煩。特別是,當我到達音頻文件的末尾時,我想通知調用AUGraphStop的主線程上的方法。AUGraphStop掛起應用程序

我已經看到了在這個線程IOUnits類似的問題,但沒有解決辦法:

iOS - AudioOutputUnitStop results in app freeze and warning

當通過UI方法調用AUGraphStop,我沒有問題,立即停止AUGraph。但是,當我從呈現回調調用AUGraphStop時,應用程序會暫停一會兒並阻止用戶交互。它還會向控制檯吐出一些警告消息。

我的代碼:

回調函數:

static OSStatus playbackCallback(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, 
           const AudioTimeStamp *inTimeStamp, 
           UInt32 inBusNumber, 
           UInt32 inNumberFrames, 
           AudioBufferList *ioData) 
{ 
//working audio playback code 
//test for end of file 
if(actualFramesRead == 0){ 
    currentMgr.endOfFile = YES; //currentMgr is reference to class managing AUGraph 
    [currentMgr notifyAudioFinished]; //this method calls a main thread 
    } 
return noErr; 
} 

通知方法:

-(void) notifyAudioFinished { 
    NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:  AUDIO_FINISHED], @"code",nil]; 
    [self performSelectorOnMainThread:@selector(stopEverything) withObject:nil waitUntilDone:YES]; 
} 

而且stopEverything是調用AUGraphStop一個類的方法。

在控制檯的警告/錯誤信息如下:這裏

WARNING: [0x3c5d718c] 1225: AURemoteIO::Stop: error 0x10004003 calling TerminateOwnIOThread 
ERROR:  [0x3830000] >aurioc> 1455: [email protected]: IOThread exiting with error 0x10004002 

我的想法是使用performSelectorOnMainThread真正停止圖形,因爲沒有掛斷,當我使用的應用程序的用戶界面,這在主線程上運行,停止它。不過,我認爲這個問題可能與從render回調中啓動這個方法有關。

任何想法,或建議更好的做法,這將不勝感激。

回答

0

所以我似乎已經解決了這個問題,儘管它並不完全清楚。

在我的課堂管理讀取音頻文件,我把它上傳到我的班級管理的音頻播放通知,當它運行的樣本:

[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationEndOfAudioFile 
                object:self userInfo:nil]; 

在我的播放類,我處理這樣的通知:

-(void) endOfAudioFileNotification: (NSNotification *) notification{ 
    [self stopEverything]; 
} 

stopEverything方法調用AUGraphStop沒有掛。最初,我嘗試使用stopEverything,使用performSelectorOnMainThread進行調用,應用程序繼續掛起並吐出這些控制檯警告。

因此,雖然我不完全確定爲什麼,但在通知的Render回調之外停止Graph似乎解決了我的問題。如果有人想提供一些澄清,那就太好了。

+0

好嗎,'kNotificationEndOfAudioFile'是什麼? –