Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play keyboard click sound in custom keyboard?

I had created custom keyboard with UIView. However I didn't hear click sound of keyboard. So I tried following codes.But I can't hear anythings. How can I play that keyboard click sound?

NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] 
                                               pathForResource:@"Tock"
                                               ofType:@"aiff"]];
    AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
    [click setVolume:0.15f];
    [click play];

And also I tried next one.

AudioServicesPlaySystemSound(0x450);

How can I?

like image 233
Fire Fist Avatar asked Apr 24 '12 13:04

Fire Fist


2 Answers

Try this:

[[UIDevice currentDevice] playInputClick];

Note that

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.

To enable a custom input or accessory view for input clicks, perform the following two steps:

Adopt the UIInputViewAudioFeedback protocol in your input view class. Implement the enableInputClicksWhenVisible delegate method to return YES.

like image 135
Anh Avatar answered Nov 15 '22 19:11

Anh


Couldn't get any of this to work, but this worked for me:

#import <AudioToolbox/AudioToolbox.h>

AudioServicesPlaySystemSound(1104);

But still I had to subclass a UIButton and add the UIInputViewAudioFeedback Protocol to it.

like image 43
Simon Unsworth Avatar answered Nov 15 '22 19:11

Simon Unsworth