我想在使用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)
}
}