20

In the documentation of AudioServicesPlayAlertSound, it says I can disable vibration when playing a sound:

iPhone—plays the specified sound. If the user has configured the Settings application for vibration on ring, also invokes vibration. However, the device does not vibrate if your app’s audio session is configured with the AVAudioSessionCategoryPlayAndRecord or AVAudioSessionCategoryRecord audio session category.

However, I still feel vibration on my iPhone 4S (iOS 5.1.1) even after setting my category to "play and record". It rings and vibrates at the same time.

#import <AudioToolbox/AudioServices.h>
#import <AVFoundation/AVAudioSession.h>

NSError* error;
[[AVAudioSession sharedInstance]
    setCategory:AVAudioSessionCategoryPlayAndRecord
    error:&error];
if (error == nil) {
    AudioServicesPlayAlertSound(1000);
}

I've also tried AudioServicesPlaySystemSound, but the results are the same. The reason why I want to disable vibration is because I am making an app where the user must place her phone far away and the phone should not topple over due to vibration.

JoJo
  • 19,587
  • 34
  • 106
  • 162
  • did you check [this](http://stackoverflow.com/questions/7831671/playing-system-sound-without-importing-your-own) – swiftBoy Mar 20 '13 at 06:11
  • @RDC The solution from the link you posted also uses `AudioServicesPlaySystemSound`. It has the same problem of vibrating on certain sound ID's such as 1000 (email received sound effect). – JoJo Mar 20 '13 at 17:49
  • The only constant I see made public in the documentation is `kSystemSoundID_Vibrate`. How did you come up with '1000' as a legitimate thing to pass `AudioServicesPlaySystemSound()`? (It works for me also, but this seems fragile.) It seems possible that Apple hard-codes the vibration into special sounds. Since these are private sounds, it seems reasonable that Apple would not need them to follow the documentation you quoted above. – Jon Brooks Mar 25 '13 at 23:33
  • @JonBrooks The entire collection of system sound constants are here: http://iphonedevwiki.net/index.php/AudioServices – JoJo Mar 26 '13 at 17:47
  • @JoJo do you know some specific sounds that Digisocial is using? – Carl Veazey Mar 26 '13 at 19:34
  • @CarlVeazey , Digisocial uses the 1009 bell dinging sound whenever you get an in-app notification that someone has liked or commented on your posts. 1009 vibrates in my testing. – JoJo Mar 26 '13 at 22:40
  • @JoJo Hm, I don't have any friends on that app so I only was able to get that to play when I signed up. But the app bundle includes a sound that's very much like 1009, so I wonder if that's it? Are there are other system sounds it uses? I've been trying to figure out what makes the 1000-2000 system sounds unique but have so far had no luck and am wondering if they've just made reproductions of the system sounds. – Carl Veazey Mar 26 '13 at 23:10
  • Probably not much help to you (I'd comment but I don't have enough rep yet), but the vibration appears to be disabled only if you are actually recording. And the problem with that is that the sound is disabled as well. – Lewis Gordon Mar 27 '13 at 12:25

2 Answers2

8

I tested this with my iPhone 4s and it does exactly what you want.

   NSError* error;
    [[AVAudioSession sharedInstance]
     setCategory:AVAudioSessionCategoryPlayAndRecord
     error:&error];
    if (error == nil) {
        SystemSoundID myAlertSound;
        NSURL *url = [NSURL URLWithString:@"/System/Library/Audio/UISounds/new-mail.caf"];
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &myAlertSound);

        AudioServicesPlaySystemSound(myAlertSound);

    }

Use AudioServicesCreateSystemSoundID to create a SystemSoundID using the filename of the system sound. Then use AudioServicesPlaySystemSound() to play the sound with no vibration. Filenames of the other system sounds can be found at the link below.

http://iphonedevwiki.net/index.php/AudioServices

Community
  • 1
  • 1
Jeff Wolski
  • 6,332
  • 6
  • 37
  • 69
  • 1
    **NOTE:** Reading this question/answer I was worried as to whether these seemingly not officially documented/publicly supported system sounds may result in an app failing the review process. I have since discovered and noted cases in which developers have used the system sounds and their app has passed the review process. One example [here](http://stackoverflow.com/questions/12255164/are-there-any-other-ios-system-sounds-available-other-than-tock/12434551#12434551). – Elliott Mar 29 '13 at 08:44
  • 1
    This works. I would suggest calling `AudioServicesDisposeSystemSoundID` at some point to free up resources. I would also create a hash map of the sounds I've already played, so I don't recreate the same sound resource over and over. – JoJo Mar 29 '13 at 22:46
3

Have you tried playing your own sound effect instead of the built in Apple's one?

If you upload a audio file in your app, you can use it easily with the AudioToolBox framework and you don't have to worry about the vibration anymore.

#import <AudioToolbox/AudioServices.h>    

//Initialise your soundID (your app delegate is a good place for this)    
SystemSoundID soundToPlay;

NSString *path  = [[NSBundle mainBundle] pathForResource:@"<YourFileName>" ofType:@"aif"];
NSURL *pathURL = [NSURL fileURLWithPath : path];
AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &soundToPlay);

//playback
AudioServicesPlaySystemSound(soundToPlay);
Eric Genet
  • 1,260
  • 1
  • 9
  • 19
  • I've done that in the past with my own sounds with no vibration, but this time I wanted to play built in system sounds. Do you know any way to grab the [system sound files](http://iphonedevwiki.net/index.php/AudioServices)? I know the popular app Digisocial got them from somewhere. They are using a lot of system sounds without vibration. – JoJo Mar 26 '13 at 17:50
  • I see. I'll do a few more tests! – Eric Genet Mar 26 '13 at 18:35