我試圖對聲音進行非常精確的定時觸發。帶音頻的定時器精度
我用這個代碼來創建計時器:
timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0));
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1ull*500000000, 5000ull);
dispatch_source_set_event_handler(timer, ^{[myAVAudioPlayer play];});
dispatch_resume(timer);
myAVAudioPlayer初始化這樣的:
NSURL *soundURL = [NSURL fileURLWithPath:[mainBundle pathForResource:@"clock" ofType:@"aiff"]];
myAVAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
[myAVAudioPlayer prepareToPlay];
我也曾嘗試使用的.caf聲音的格式和蘋果的SoundEffect類BubbleLevel示例代碼,在性能上沒有顯着差異。
如果發射率很低,聲音相當準確。但是,如果我加快發射速度,聲音非常不準確。
任何幫助將不勝感激。
我有同樣的問題,你找到一個解決方案? – QuiBongJin