1

Up until today I have been testing my apps in the Simulator. For the first time I tested a project on a real iPhone. I was unpleasantly surprised that every time I press a button the phone vibrates. This wouldn't be a big deal except that my particular app has a built in keyboard that plays an audio sound every time a key (UIButton) is pressed.

enter image description here

The combination of the vibration sound plus the audio sound is very distracting. Is there any way to programmatically disable button vibration? I'm not asking how to disable all vibration for the phone, just the buttons I choose. The only relevant question I could find was just the opposite: Making the iPhone vibrate.

I don't have to leave it up to the user to turn vibration off in settings, do I?

Update 1:

Judging from the comments, maybe this is either a device specific problem or I am somehow triggering a vibration.

This is how I play a sound:

import Foundation
import AudioToolbox

class Player {
    
    private let audioFolder = "raw"
    
    func playSoundFromFile(file: String) {
        var soundURL: NSURL?
        var soundID: SystemSoundID = 0
        let filePath = NSBundle.mainBundle().pathForResource("\(audioFolder)/\(file)", ofType: "mp3")
        soundURL = NSURL(fileURLWithPath: filePath!)
        if let url = soundURL {
            AudioServicesCreateSystemSoundID(url, &soundID)
            AudioServicesPlayAlertSound(soundID)
        }
    }
}

And this is my button tap action:

@IBAction func keyTapped(sender: UIButton) {
    
    let buttonText = sender.titleLabel?.text ?? ""
    
    updateDisplayForIpa(buttonText)
    
    // play sound
    if let fileName = singleSound.fileNameForIpa(buttonText) { // converts IPA sound symbol into a filename 
        player.playSoundFromFile(fileName)
    }
    
}

Since the tap calls an update display method, here that is:

func updateDisplayForIpa(ipa: String) {
    
    if let fileName = singleSound.fileNameForIpa(ipa) {
        
        ipaLabel.text = ipa // UILabel
        ipaDescription.text = "\(fileName)_description".localized // UITextView
        ipaDescription.scrollRangeToVisible(NSRange(location: 0, length: 0))
        example1.setTitle("\(fileName)_example1".localized, forState: UIControlState.Normal) // UIButton
        example2.setTitle("\(fileName)_example2".localized, forState: UIControlState.Normal) // UIButton
        example3.setTitle("\(fileName)_example3".localized, forState: UIControlState.Normal) // UIButton
        
    }
}

I don't see anything here that would trigger a vibration...

Update 2

I created a new project and added a single UIButton to the storyboard. I tested it on the same phone that vibrates in the above app. There was no vibration when I tapped the button, so there must be something about the above code that is triggering a vibration. But what could it be?

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • Might I recommend that if the behavior of your app stacked on top of the default/built-in behaviors of the device or OS are annoying, then it might be best to adjust what your app is doing instead of breaking the user's device or OS expectations with the idea that your app is a special-snowflake... – nhgrif Oct 10 '15 at 13:20
  • Those sound like wise words, as much as I hate to hear them. Would you recommend that I replace `UIButton` with a `UILabel`? – Suragch Oct 10 '15 at 13:29
  • 1
    No. I mean, unless your buttons are just labels? I haven't experienced this vibration. I don't know if it's OS specific or device specific. But it's limited to keyboards, right? – nhgrif Oct 10 '15 at 13:31
  • I haven't ever experienced vibration from a button. How are you playing the sound? You must be triggering a vibration somehow. – Paulw11 Oct 10 '15 at 13:39
  • @Paulw11, You're right. Somehow my code is triggering a vibration. When I create a fresh project with a single `UIButton`, tapping that button does not cause a vibration. I'm using `AudioServicesPlayAlertSound` to play sounds. – Suragch Oct 12 '15 at 00:46
  • The answer from @Wevah solved the problem. I should have been using `AudioServicesPlaySystemSound`. – Suragch Oct 12 '15 at 00:55

1 Answers1

4

AudioServicesPlayAlertSound can cause a vibration:

iPhone—plays the specified sound. If the user has configured the Settings application for vibration on ring, also invokes vibration.

I'd try AudioServicesPlaySystemSound instead:

To play a short sound not used as an alert, use AudioServicesPlaySystemSound.

Wevah
  • 28,182
  • 7
  • 83
  • 72