Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play system sound on iOS without vibration?

Tags:

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.

like image 765
JoJo Avatar asked Mar 19 '13 23:03

JoJo


People also ask

How do I make my iPhone stop vibrating when I put the speaker on?

Set vibration options for specific alerts: Go to Settings > Sounds & Haptics. See Change iPhone sounds and vibrations. Turn off all vibrations: Go to Settings > Accessibility > Touch, then turn off Vibration. Note: This setting turns off vibrations for earthquake, tsunami, and other emergency alerts.

What is play system Sounds on iPhone?

System Sound Services provides a C interface for playing short sounds and for invoking vibration on iOS devices that support vibration. You can use System Sound Services to play short (30 seconds or shorter) sounds.

What is System Soundservice?

System Sounds is the audio produced directly by your operating system – such as Windows and OS X startup and shutdown sounds, error notifications, and other standard notifications.


1 Answers

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

like image 186
Jeff Wolski Avatar answered Sep 21 '22 19:09

Jeff Wolski