播放声音无延迟iOS
我找不到方法来以低延迟播放真实声音.
I can't find method how i can play sound real with low latency.
我尝试使用AVFoundation音频播放器在500毫秒左右的巨大延迟
I try use AVFoundation audio player huge latency around 500ms
因此,我尝试创建系统声音,并且也没有200ms左右的运气延迟,这对我来说并不多,但没有用.我最多需要50毫秒.
So i try create system sound, and too without luck latency around 200ms it's not much but not useful for me. I need 50ms max.
请确保我的声音采样音色清晰无声.
Be sure my sound sample is clear tone without silence.
SystemSoundID cID;
BOOL spinitialized;
-(IBAction)doInit
{
if (spinitialized){
AudioServicesPlaySystemSound (cID);
return;
}
NSURL *uref = [[NSURL alloc] initFileURLWithPath: [NSString stringWithFormat:@"%@/soundlib/1.wav", [[NSBundle mainBundle] resourcePath]]];
OSStatus error = AudioServicesCreateSystemSoundID ((__bridge CFURLRef)uref, &cID);
if (error) NSLog(@"SoundPlayer doInit Error is %d",(int)error);
AudioServicesPlaySystemSound (cID);
spinitialized = YES;
}
所以我尝试通过按下按钮来拨打电话.
So i try call by button press down.
使用已运行的RemoteIO音频单元(或AVAudioUnit)以及已加载到内存中的PCM波形数据,可提供最低的延迟方法,以便在iOS设备上产生声音.
Using an already running RemoteIO Audio Unit (or AVAudioUnit) with PCM waveform data that is already loaded into memory provides the lowest latency method to produce sound on iOS devices.
由于存在缓冲,不可能实现零延迟,但是在所有当前的iOS设备上,缓冲大小通常为5.3到5.8毫秒或更小.在最新的iOS设备上,您可以更频繁地获取音频回调.您的音频回调代码必须准备好手动将所需波形数据的适当顺序片段复制到音频缓冲区中.它将在非UI线程中调用,因此回调必须是线程安全的,并且不执行任何锁定,内存管理甚至是Objective C消息传递.
Zero latency is impossible due to buffering, but on all current iOS devices, the buffer size is usually 5.3 to 5.8 milliseconds or lower. On the newest iOS devices you can get audio callbacks even more often. Your audio callback code has to ready to manually copy the proper sequential slice of the desired waveform data into an audio buffer. It will be called in a non-UI thread, so the callback needs to be thread safe, and do no locks, memory management or even Objective C messaging.
使用其他AV音频播放方法可能会导致更长的延迟,这是因为将声音加载到内存中(包括潜在的拆包或解压缩)并打开音频硬件(等)以及通常情况下所花费的时间.使用更长的音频缓冲区.即使启动RemoteIO音频单元也有其自身的延迟.但是它可以提前启动,可能会产生静音,直到您的应用在收到某些事件后需要以尽可能低的延迟(但非零延迟)播放声音.
Using other AV audio playing methods may result in far higher latency due to the time it takes to load the sound into memory (including potential unpacking or decompression) and to power up the audio hardware (etc.), as well as typically using longer audio buffers. Even starting the RemoteIO Audio Unit has its own latency; but it can be started ahead of time, potentially playing silence, until your app needs to play a sound with the lowest possible (but non-zero) latency, upon receiving some event.