16

I need to vibrate iOS device, devices that don’t support vibration, Will plays a beep sound.

For this I am using

Import AudioToolbox.framework
#import <AudioToolbox/AudioToolbox.h>

AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

It vibrate on iPhone device, but No sound on iPad and iPod. While refrences say that it will play sound. What I am doing wrong ?

Making the iPhone vibrate

http://blog.mugunthkumar.com/coding/iphone-tutorial-better-way-to-check-capabilities-of-ios-devices/

Community
  • 1
  • 1
Mangesh
  • 2,257
  • 4
  • 24
  • 51
  • The [documentation](https://developer.apple.com/library/ios/documentation/AudioToolbox/Reference/SystemSoundServicesReference/Reference/reference.html) seems to indicate that the iPod Touch, at least, should do nothing: "Constants kSystemSoundID_Vibrate On the iPhone, use this constant with the AudioServicesPlayAlertSound function to invoke a brief vibration. On the iPod touch, does nothing." – borrrden Jul 08 '14 at 10:31
  • Thanks @borrrden, You are looking into AudioServicesPlaySystemSound while I am using AudioServicesPlayAlertSound, Documentation say "Depending on the particular iOS device, this function plays a short sound and may invoke vibration. Calling this function does the following on various iOS devices:" – Mangesh Jul 08 '14 at 10:31
  • I realized that, and updated my comment with a different section from the documentation – borrrden Jul 08 '14 at 10:32
  • Thanks, but it say 1)AudioServicesPlayAlertSound(kSystemSoundID_Vibrate); 2)AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); Both the functions vibrate the iPhone. But when you use the first function on devices that don’t support vibration, it plays a beep sound. The second function on the other hand does nothing on unsupported devices. and I need to vibrate device and if no Hardware support then play beep sound. – Mangesh Jul 08 '14 at 10:42
  • 3
    Sorry to ask, but ... Have you checked that the devices are not muted? Stumbled upon that myself and wondered why the device didn't vibrate until I noticed that the mute switch was on :) – Cornelius Jul 08 '14 at 12:10
  • @Mangesh I trust the documentation more than a three year old stack overflow answer. – borrrden Jul 09 '14 at 01:28
  • i tried this objective c code but the iphone doesnt vibrate, using latest xcode version, latest iphone Os and iphone 7 – user2899094 Jun 15 '20 at 13:58

1 Answers1

5

Try setting the AudioSession:

#import <AudioToolbox/AudioToolbox.h>

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;         
[audioSession setCategory: AVAudioSessionCategoryPlayback  error:&err];
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);         
//or: AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
lukaswelte
  • 2,951
  • 1
  • 23
  • 45