可以在Swift的SpriteKit应用中使用语音识别吗?

时间:2019-05-31 00:18:58

标签: swift xcode sprite-kit

我正在使用SpriteKit来构建应用程序。场景的其他功能全部通过SpriteKit完成。我想在场景中添加语音识别,以便当用户触摸麦克风按钮节点时,他们说出的单词会与他们继续说下去所需的正确单词匹配。这在SpriteKit上可行吗?下面的代码是我正在使用的函数,但没有引起任何事情。

func recordAndRecognizeSpeech() {
    let node = audioEngine.inputNode
    let recordingFormat = node.outputFormat(forBus: 0)
    node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { buffer, _ in
        self.request.append(buffer)
    }
    audioEngine.prepare()
    do {
        try audioEngine.start()
    } catch {
        self.sendAlert(message: "There has been an audio engine error.")
        return print(error)
    }
    guard let myRecognizer = SFSpeechRecognizer() else {
        self.sendAlert(message: "Speech recognition is not supported for your current locale.")
        return
    }
    if !myRecognizer.isAvailable {
        self.sendAlert(message: "Speech recognition is not currently available. Check back at a later time.")
        // Recognizer is not available right now
        return
    }
    recognitionTask = speechRecognizer?.recognitionTask(with: request, resultHandler: { result, error in
        if let result = result {

            let bestString = result.bestTranscription.formattedString
            self.labelNode.text = bestString

            var lastString: String = ""
            for segment in result.bestTranscription.segments {
                let indexTo = bestString.index(bestString.startIndex, offsetBy: segment.substringRange.location)
                lastString = bestString.substring(from: indexTo)
            }
            self.checkPhrase(resultString: lastString)
        } else if let error = error {
            self.sendAlert(message: "There has been a speech recognition error.")
            print(error)
        }
    })
}

下面是当麦克风节点被触摸而我运行时所使用的代码:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let touch = touches.first else { return }
    let location = touch.location(in: self)


    if micButton.contains(location) {
        if isRecording == true {
            audioEngine.stop()
            recognitionTask?.cancel()
            isRecording = false
        } else {
            self.recordAndRecognizeSpeech()
            isRecording = true
        }


    }

1 个答案:

答案 0 :(得分:0)

您可以使用实例方法 requestRecordPermission(_:)来请求用户进行录音的权限。

尝试类似的东西:

if (session.respondsToSelector("requestRecordPermission:")){
    AVAudioSession.sharedInstance().requestRecordPermission({(granted: Bool)-> Void in
        if granted {
            println("granted")
            session.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)
            session.setActive(true, error: nil)
            self.recorder ()
        }else{
            println("not granted")
        }
    })
}   session.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)
        session.setActive(true, error: nil)
            self.recorder ()
        } else{
            println("not granted")
        }
     })
}