1

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

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
ICoffeeConsumer
  • 882
  • 1
  • 8
  • 22

2 Answers2

1

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?

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
nsdebug
  • 364
  • 2
  • 8
  • Very good theory, but changing the function didn't fix it. Thanks though – ICoffeeConsumer Mar 15 '14 at 19:00
  • I suspect that NSLog is changing things just enough to cause it to work in that case. Instead of NSLog, try using Xcode's ability to set a breakpoint and print output. This way, you avoid the NSLog - which changes your code - but you can still break (and spit out text and auto-continue.) Simply right-click on breakpoint in Xcode to alter these settings. Hope this helps. – nsdebug Mar 15 '14 at 19:06
0

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.

Community
  • 1
  • 1
ICoffeeConsumer
  • 882
  • 1
  • 8
  • 22
  • That is stated in the documentation of AudioServicesPlayAlertSound, it says: .... However, the device does not vibrate if your app’s audio session is configured with the AVAudioSessionCategoryPlayAndRecord or AVAudioSessionCategoryRecord audio session category. This ensures that vibration doesn’t interfere with audio recording..... – theguy Jan 11 '17 at 12:24