20
votes

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.

2
did you check this - swiftBoy
@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
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
@JonBrooks The entire collection of system sound constants are here: iphonedevwiki.net/index.php/AudioServices - JoJo
@JoJo do you know some specific sounds that Digisocial is using? - Carl Veazey

2 Answers

8
votes

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

3
votes

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);