Hol*_*bæk 8 iphone audio objective-c ios ios6
当人们按下我的应用程序中的某些按钮时,我想播放一些简单的声音效果,我尝试了几种,但是我总是会遇到延迟,使声音看起来不同步.
我已经按照这里的教程,所以我尝试了音频服务的构建,但是有一个延迟,我尝试了AVAudioPlayer,但也有延迟,即使我使用"prepareToPlay".
我是否真的必须安装一个像Finch这样庞大且凌乱的库来获得简单的声音效果而且在我的简单应用程序中没有延迟?
希望你能为我澄清一些事情!
UPDATE
音频服务代码:
NSURL *soundURL = [[NSBundle mainBundle] URLForResource:@"PlopsX4"
withExtension:@"aif"];
AudioServicesCreateSystemSoundID((CFURLRef)soundURL, &sound1);
AudioServicesPlaySystemSound(sound1);
Run Code Online (Sandbox Code Playgroud)
viewDidLoad中AVAudioPlayer的代码:
NSURL *soundURL = [[NSBundle mainBundle] URLForResource:@"PlopsX4"
withExtension:@"aif"];
self.avSound = [[AVAudioPlayer alloc]
initWithContentsOfURL:soundURL error:nil];
[self.avSound prepareToPlay]
Run Code Online (Sandbox Code Playgroud)
我要播放文件的方法中的AVAudioPlayer代码:
[self.avSound play];
Run Code Online (Sandbox Code Playgroud)
我让它工作的方法(我想我是从另一篇关于SO的文章中得到这个想法的)是创建一个空的1秒.wav文件并在初始化时“播放”它。之后再调用其他音效就没有任何延迟了。
我的声音播放器对象中有类似的东西
SystemSoundID blID;
SystemSoundID cID;
+(void)doInit
{
if (spinitialized){
AudioServicesPlaySystemSound (blID);
return;
}
NSString *str = [[NSBundle mainBundle] pathForResource:@"ding.wav" ofType:@""];
NSURL *uref = [NSURL fileURLWithPath:str];
OSStatus error = AudioServicesCreateSystemSoundID ((CFURLRef)uref, &cID);
if (error) NSLog(@"SoundPlayer doInit Error is %ld",error);
str = [[NSBundle mainBundle] pathForResource:@"blank.wav" ofType:@""];
uref = [NSURL fileURLWithPath:str];
error = AudioServicesCreateSystemSoundID ((CFURLRef)uref, &blID);
if (error) NSLog(@"SoundPlayer doInit Error is %ld",error);
AudioServicesPlaySystemSound (blID);
spinitialized = YES;
}
Run Code Online (Sandbox Code Playgroud)
然后我可以使用以下方法立即播放“ding”而无需任何延迟:
AudioServicesPlaySystemSound (cid);
Run Code Online (Sandbox Code Playgroud)