简体   繁体   中英

NSOperationQueue Heisenbug?

I'm working on an app that calls AudioServicesPlayAlertSound(kSystemSoundID_Vibrate) from a loop in a separate thread launched with performSelectorInBackground . For some reason, the app only vibrates reliably when the entire app is running in the background. I tried calling the vibrate function from a block sent to the main NSOperationQueue, but it's still only consistent when in the background. Unless, of course, I throw a NSLog() call into the operation block:

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
    NSLog(@"VIBRATING");
    AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
}];

With the NSLog call, it runs as intended regardless of where it's running.

Any idea why this is? All suggestions are much appreciated

The header file documentation for AudioServicesPlayAlertSound states

Play the provided SystemSoundID with AlertSound behavior.

I would imagine that it simply does not play while the app is considered active and front-most. Maybe try using AudioServicesPlaySystemSound instead?

One answer to Making the iPhone vibrate solved my problem. For some reason, the iPhone doesn't like to vibrate while recording audio. So the obvious solution is to stop recording before vibrating:

[_recorder stop];
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
[_recorder record];

Still, I'm not sure why the NSLog has any effect, and I still don't see why it worked just fine without the above fix while running in the background.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM