How to play keyboard click sound in custom keyboard?

后端 未结 3 949
囚心锁ツ
囚心锁ツ 2020-12-16 13:29

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 pla

相关标签:
3条回答
  • 2020-12-16 14:01

    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.

    0 讨论(0)
  • 2020-12-16 14:24

    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.

    0 讨论(0)
  • 2020-12-16 14:25

    In Swift add an extension to the input view as follows;

    extension UIInputView : UIInputViewAudioFeedback {
    
        public var enableInputClicksWhenVisible: Bool {
            return true
        }
    
    }
    

    In the action method for your buttons call the following function:

        UIDevice.current.playInputClick()
    

    Add the following property to your info.plist:

    NSExtension | NSExtensionAttributes | RequestsOpenAccess = YES

    0 讨论(0)
提交回复
热议问题