我有以下代码:
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;
}
];
我该怎么做才能在不影响其余代码的情况下播放声音?我根本不想播放声音来暂停代码?我如何更改代码以便异步播放声音而不影响其余代码?谢谢!
最佳答案
是什么让您认为这会暂停代码?文档说:
Because sound might play for several seconds, this function is executed asynchronously.
如果这里确实有东西阻塞,我猜它是 AudioServicesCreateSystemSoundID()
。但即使这不会阻塞,每次你想播放声音时都创建其中一个可能不是一个好主意,尤其是如果你从未处理过它们。您应该在某种设置中创建一次,然后随时使用 AudioServicesPlaySystemSound()
播放它。
关于ios - 异步声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14449344/