2012-08-11 57 views
1

我想知道在OpenAL中使用AVAudioPlayerDelegateaudioPlayerDidFinishPlaying:successfully:方法的效果。例如:確定OpenAL何時使用回調完成播放音頻

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag 
{ 
    // (code or conditions for when the audio has finished playing...) 
} 
+0

[在OpenAL中播放聲音時得到通知]可能的重複(http://stackoverflow.com/questions/1046315/getting-notified-when-a-sound-is-done-playing-in-openal ) – bobobobo 2013-09-11 03:16:33

回答

2

一般來說,OpenAL的不會通知您,當音頻播放完畢,所以沒有真正相當於AVAudioPlayerDelegate。最簡單的方法是簡單地按照音頻的長度延遲一個函數/塊的調用。舉個例子,你可以使用libdispatch(又名大中央調度)來設定的時間量後的塊添加到隊列:

dispatch_time_t delay; 
dispatch_queue_t queue; 
dispatch_block_t block; 
uint64_t audio_length_ns = 10000000000; // 10 seconds 

delay = dispatch_time(DISPATCH_TIME_NOW, audio_length_ns); 
queue = dispatch_get_main_queue(); 

block = ^{ 
    // Do whatever you need to after the delay 
    // Maybe check to see if the audio has actually 
    // finished playing and queue up the block again 
    // if it hasn't. 
}; 

// Queue up the block for the time after 
dispatch_after(delay, queue, block); 

稍硬的方法是,如在塊內的註釋中提到,檢查OpenAL是否在塊中完成,如果不是,則再次將塊推入隊列(可能延遲時間較短,特別是如果您可以近似估計時間的話)。不過,一般來說,你可能不需要專注,只是在一個合適的聲音範圍內完成就夠了。

您也可以通過其他方法安排這類事情,例如performSelector:withObject:afterDelay:,但是就API而言,這更多地取決於您的偏好。這個想法幾乎是一樣的。