异步声音

时间:2013-01-21 23:58:01

标签: ios objective-c cocoa-touch ios4 ios6

我有以下编码:

SystemSoundID soundID;
NSString *soundFile = [[NSBundle mainBundle] pathForResource: _sound ofType:@ "wav"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef) [NSURL fileURLWithPath:soundFile], &soundID);
AudioServicesPlaySystemSound(soundID);


    [UIView animateWithDuration: 0.6f
                          delay: 0.1f
                        options: UIViewAnimationTransitionCurlUp
                     animations:^{
                         [appDelegate.presstobook setFrame:  CGRectMake(136, appDelegate.window.frame.size.height - 50, 47, 47)];
                     }
                     completion:^(BOOL finished){

                         presstobook = NO;
                     }
     ];

如何才能在不影响其余代码的情况下播放声音?我不想播放声音暂停代码?如何更改代码以便在不影响其余代码的情况下异步播放声音?谢谢!

1 个答案:

答案 0 :(得分:2)

是什么让你觉得这会暂停代码?文档说:

  

因为声音可能会播放几秒钟,所以此功能是异步执行的。

如果这里的某些内容阻止了,我猜它是AudioServicesCreateSystemSoundID()。但即使这不会阻止,每次想要播放声音时创建其中一个也许不是一个好主意,特别是如果你从未处理它们。您应该在某种设置中创建一次,然后随时使用AudioServicesPlaySystemSound()播放它。