Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在自定义键盘中播放自定义声音_Ios_Swift_Audio_Keyboard - Fatal编程技术网

Ios 如何在自定义键盘中播放自定义声音

Ios 如何在自定义键盘中播放自定义声音,ios,swift,audio,keyboard,Ios,Swift,Audio,Keyboard,我想在使用swift单击自定义键盘上的键时播放音频文件。我试过这个,但是没有运气。我怎样才能做到这一点?在键盘上播放自定义音频是否可行 func btnPressed(sender: AnyObject) { var btn = sender as UIButton var stringToInsert="" var proxy = self.textDocumentProxy as UIKeyInput switch (btn.tag) { c

我想在使用swift单击自定义键盘上的键时播放音频文件。我试过这个,但是没有运气。我怎样才能做到这一点?在键盘上播放自定义音频是否可行

func btnPressed(sender: AnyObject)
{
    var btn = sender as UIButton
    var stringToInsert=""
    var proxy = self.textDocumentProxy as UIKeyInput

    switch (btn.tag)
    {
    case 10:
        stringToInsert = "a"
        if let path = NSBundle.mainBundle().pathForResource("8bit", ofType: "wav") {
            audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path), fileTypeHint: "wav", error: nil)

            if let sound = audioPlayer {
                sound.prepareToPlay()
                sound.play()
                println("hello") 
            }
        }
    }
    proxy.insertText(stringToInsert)  
}
}

这个代码对我来说很好用。但是在你的代码中有一些小的改变。将let sound=audioPlayer作为AVAudioPlayer?与其让sound=audioPlayer试一试,不如让它解决您的问题:谢谢但我还是不能让它工作。该键的作用就像播放剪辑,降低不透明度并在按下状态下保持几秒钟,但不会产生声音。可能无法从键盘播放音频片段?