16

I have implemented an alternative keyboard on my first iPhone app. I would like to play a sound when a button is pressed. The default click sound when the standard keyboard is pressed would be fine, but I don't know how to get that sound.

In this answer I found how to play a 'tock' sound:

Playing system sound without importing your own

I got this to work easily enough but it is a very loud sound. I've searched Stack Overflow and the Xcode help for available iOS system sounds other than 'tock' and I can't find them.

Are there other built-in sounds, maybe even the default keyboard sound, that I can use?

Community
  • 1
  • 1
Dale Dietrich
  • 7,196
  • 4
  • 21
  • 25
  • 1
    If the question would really be what its title suggests, I,d dare answering 'there's AVSystemSoundID_Vibrate'. –  Sep 04 '12 at 05:28

2 Answers2

18

Prior to, and since posting this question almost 2 weeks ago I have searched high and low for a method to make the keyboard click sound work in my app. I have attempted to implement and understand Apple's documentation on custom input views. I butchered my app in many ways attempting to make it work and created several test projects. I tried to interpret how to do this from various other sources on the web. All to no avail. I have finished my app all but for this last function.

I finally hit on a simple answer towards the end of this IPhone Dev SDK form question/post. The ultimate answer is easy and can be done in a few seconds with these easy steps.

  1. Link the 'AudioToolbox.framework' to your project (Click on your project's Target, select 'Build Phases' and under the 'Link Binary with Libraries' item click the + button and add the framework.

  2. Add the following to the header file of the class where you want to implement the click:

     #import <AudioToolbox/AudioToolbox.h>
    
  3. Add the following line of code in the implementation file of your class wherever you want the keyboard click sound to appear:

     AudioServicesPlaySystemSound(0x450);
    

THAT'S IT! AND IT WORKED!!! HALLELUJAH!

According to Paul in the other forum this has the following downside:

"keyboard keys won't be affected by the iOS user preference for keyboard sound on/off."

But when I turn the volume up or down on my iPhone or if I mute the iPhone, the keyboard click sounds do go up and down or are muted as would be expected. Given that I found it impossible to implement keyboard clicks "UIInputViewAudioFeedback" as documented, I'm happy to have anything work.

P.S. If anyone has a link to an actual project that I can download, build and run on my iPhone to see EXACTLY how to properly implement: "UIInputViewAudioFeedback" I'd be happy to give it a look. But so far I've found none, nor any step-by-step newbie style instructions on how to make it work.

[Sept 30, 2012 UPDATE:] I'm happy to say that my app was accepted into the app store using this method. Here it is: https://itunes.apple.com/app/fine-tip-tip-calculator/id563429225

...Dale

Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
Dale Dietrich
  • 7,196
  • 4
  • 21
  • 25
  • I had given up on finding a solution for this. Thank you for posting. – d.altman Nov 09 '12 at 03:08
  • 4
    Here is a link with all the sound codes you can input into the method. http://iphonedevwiki.net/index.php/AudioServices – M Jesse Feb 21 '13 at 05:31
  • @Dale Dietrich I am sorry,I followed every step of yours,but still couldn't play any sound for button click – Eshwar Chaitanya Jun 26 '13 at 10:32
  • Works great for input events, but it sounds awful for wheels, like the datepicker, the sounds overlay each other, thanks anyway! – 3lvis Apr 17 '14 at 12:10
  • Is there really no constant that can be used in place of `0x450` ?! – Olie Feb 20 '15 at 00:40
  • Thanks, Dale. One thing I noticed on iOS 8.1.3/iPhone 6 is that I was able to play: AudioServicesPlaySystemSound(1114); // = "endrecord.caf" while the video camera was on. I was unable to get it to play via the various other sound-playback methods. Also NOTE: most sounds in the iphondevwiki (link in above comment) would not play in that scenario. – Jason R. Escamilla Mar 09 '15 at 11:16
  • Check out this answer. Maybe it is useful to you playing sounds without using the audiotoolbox: https://stackoverflow.com/a/39385287/1306012 – Bruno Bieri Jan 07 '22 at 16:20
7

Try UIDevice's playInputClick:

[[UIDevice currentDevice] playInputClick];

From the documentation:

Use this method to play the standard system keyboard click in response to a user tapping in a custom input or keyboard accessory view. A click plays only if the user has enabled keyboard clicks in Settings > Sounds, and only if the input view is itself enabled and visible.

Make sure you adopt the UIInputViewAudioFeedback protocol in your input view class, and implement the enableInputClicksWhenVisible delegate method to return YES, as per the documentation.

EDIT - For implementation of delegate methods, check out Apple's documentation on custom input views.

Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
mopsled
  • 8,445
  • 1
  • 38
  • 40
  • Thanks mopsled! I'm a bit over my head. I can't get this to work. Here's what I've done so far. In my view *.h file I added **bolded text**: '@interface TipMainViewController : UIViewController ' '@property (nonatomic, readonly) BOOL enableInputClicksWhenVisible;' In my view *.m file I added: '@synthesize enableInputClicksWhenVisible;' '- (BOOL) enableInputClicksWhenVisible { return YES; }' and inserted the following where I wanted the tick sound: '[[UIDevice currentDevice] playInputClick];' What did I do wrong? – Dale Dietrich Sep 04 '12 at 04:44
  • SORRY, I tried and tried to format the above with some structure but the commenting system here doesn't seem to permit it. :( – Dale Dietrich Sep 04 '12 at 04:54
  • Your implementation is close, but you shouldn't be adding any `@property` or `@synthesize` statements to implement a delegate. Also, you should be implementing the protocol on the input view, not the view controller. Check out the link I added to my answer above. – mopsled Sep 04 '12 at 05:27
  • OK, but I do not have a separate keyboard input view. I am using a TextField that I dragged onto the view controller's xib file. I hide the normal keyboard and implement a keyboard through buttons that I also added on to the view controller's xib file. I did read the excellent tutorial you linked to in the "EDIT" field above. Thinking I was following its instructions I went down the road of creating a separate UITextField Class called 'KeyboardClick'. Using the Identity Inspector in Interface builder I linked the applicable input Text Field to the new KeyboardClick Class. [OUT OF SPACE :(] – Dale Dietrich Sep 04 '12 at 19:03
  • I must confess that I don't fully understand protocols and delegation. Do I need to instantiate the new KeybardClick class? I tried doing this but nothing happened. Plus xCode said it was an unused variable. I'm getting discouraged. – Dale Dietrich Sep 04 '12 at 19:14