简体   繁体   English

iOS Swift自定义键盘没有声音和延迟

[英]iOS swift custom keyboard no sound and delay

I´m testing the keyboard extension with new language swift on iOS, but I can´t play a sound for keyPressed event, and there is a delay, about 10 seconds when I click the key. 我正在用iOS上的新语言swift测试键盘扩展,但是我无法播放keyPressed事件的声音,并且会有延迟,当我单击键时大约有10秒。

This is my code: 这是我的代码:

@IBAction func keyPressed(button: UIButton) {
    var string = button.titleLabel!.text
    (textDocumentProxy as UIKeyInput).insertText("\(string!)")
    AudioServicesDisposeSystemSoundID(1104)
    AudioServicesPlaySystemSound(1104)
    UIView.animateWithDuration(0.2, animations: {
        button.transform = CGAffineTransformScale(CGAffineTransformIdentity, 2.0, 2.0)
        }, completion: {(_) -> Void in
            button.transform =
            CGAffineTransformScale(CGAffineTransformIdentity, 1, 1)
    })
}

Thanks in advance for any comment or suggestion... 预先感谢您的任何评论或建议...

You need to play the sound in another thread. 您需要在另一个线程中播放声音。 The user needs to allow full access for this to work. 用户需要允许完全访问权限才能工作。 So make sure you request for this in the info.plist of the extension 因此,请确保您在扩展程序的info.plist中请求此请求

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),     
{
    AudioServicesPlaySystemSound(1104);
});    

For Objective-C just add the ^ sign in front of the { 对于Objective-C,只需在{前面添加^符号

您需要将AudioServicesDisposeSystemSoundID(1104)移动到deinit方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM