swift 在SpeechKit框架中连续收听用户语音并检测语音静音结束

mnowg1ta  于 2023-02-18  发布在  Swift
关注(0)|答案(2)|浏览(146)

我正在开发一个应用程序,需要根据语音命令打开某个屏幕,例如,如果用户说“打开设置”,那么它应该打开设置屏幕,到目前为止,我使用了SpeechKit框架,但无法检测语音静音的结束。就像Siri是如何做到的。我想检测用户是否结束了他的句子/短语。
请找到下面的代码,其中我以两种方式集成了SpeechKit框架。
A)过孔封闭(recognitionTask(with request: SFSpeechRecognitionRequest, resultHandler: @escaping (SFSpeechRecognitionResult?, Error?) -> Swift.Void) -> SFSpeechRecognitionTask

let audioEngine = AVAudioEngine()
let speechRecognizer = SFSpeechRecognizer()
let request = SFSpeechAudioBufferRecognitionRequest()
var recognitionTask: SFSpeechRecognitionTask?

func startRecording() throws {

        let node = audioEngine.inputNode
        let recordingFormat = node.outputFormat(forBus: 0)

        node.installTap(onBus: 0, bufferSize: 1024,
                        format: recordingFormat) { [unowned self]
                            (buffer, _) in
                            self.request.append(buffer)
        }

        audioEngine.prepare()
        try audioEngine.start()

        weak var weakSelf = self

        recognitionTask = speechRecognizer?.recognitionTask(with: request) {
            (result, error) in

            if result != nil {

                if let transcription = result?.bestTranscription {
                    weakSelf?.idenifyVoiceCommand(transcription)
                }
            }
        }            
}

但是当我说任何像“Open Setting”这样的单词/句子时,闭包(recognitionTask(with:))被调用了多次,并且我已经将方法(idenifyVoiceCommand)放入了多次调用的闭包中,所以我如何限制只能调用一次。
我还审查了计时器逻辑,而谷歌它(SFSpeechRecognizer - detect end of utterance),但在我的场景,它不工作,因为我没有停止音频引擎,因为它不断听取用户的声音一样,Siri没有。
B)通过代表(SFSpeechRecognitionTaskDelegate
语音识别器。识别任务(带有:自我请求,委托:自我)

func speechRecognitionTaskWasCancelled(_ task: SFSpeechRecognitionTask) {

}

func speechRecognitionTask(_ task: SFSpeechRecognitionTask, didFinishSuccessfully successfully: Bool) {

}

而且我发现处理演讲结束的委托有时不调用它,有时在演讲结束后意外地调用它。

0vvn1miw

0vvn1miw1#

我之前也有同样的问题。
我检查了你的问题,我想下面的代码可以帮助你实现我所做的同样的事情:

recognitionTask = speechRecognizer?.recognitionTask(with: recognitionRequest, 
resultHandler: { (result, error) in

    var isFinal = false

    if result != nil {

        self.inputTextView.text = result?.bestTranscription.formattedString
        isFinal = (result?.isFinal)!
    }

    if let timer = self.detectionTimer, timer.isValid {
        if isFinal {
            self.inputTextView.text = ""
            self.textViewDidChange(self.inputTextView)
            self.detectionTimer?.invalidate()
        }
    } else {
        self.detectionTimer = Timer.scheduledTimer(withTimeInterval: 1.5, repeats: false, block: { (timer) in
            self.handleSend()
            isFinal = true
            timer.invalidate()
        })
    }

})

这将检查是否在1.5秒内未接收到输入

jgwigjjp

jgwigjjp2#

向语音识别器类添加:

private var timer : Timer?

并在此处修改代码:

recognitionTask = speechRecognizer.recognitionTask(with: request) { (result, error) in
        self.timer?.invalidate()
        self.timer = Timer.scheduledTimer(withTimeInterval: 1.5, repeats:false) { _ in
                       self.timer = nil
                       //do here what do you want to do, when detect pause more than 1.5 sec
                   }
        if result != nil {

相关问题