gpt4 book ai didi

ios - Swift:在沉默 x 秒后停止语音识别

转载 作者:搜寻专家 更新时间:2023-10-30 21:58:07 26 4
gpt4 key购买 nike

我一直在寻找解决方案,但我看到的所有解决方案都令人困惑,所以我想我会提出一个新问题。

我正在使用语音库,我希望识别任务在 2 秒后没有用户输入时停止。我知道我想使用计时器,但我无法弄清楚将它放在哪里以及如何更新它。

我在按下录制按钮时启动计时器,并在按下停止录制按钮时使它无效。

但是我在哪里检查用户是否添加了新的输入?我正在考虑保存最后一个转录并将其与下一个进行比较:当它们不同时,重置计时器。

这是我的代码:

recognitionTask = speechRecognizer.recognitionTask(with: recognitionRequest) { result, error in
var isFinal = false

if let result = result {
self.textView.text = result.bestTranscription.formattedString
// Should I compare the result here to see if it changed?
isFinal = result.isFinal
}

// Or should I do it here? In what order is this code even running?

if error != nil || isFinal {
self.result = self.textView.text

self.audioEngine.stop()
inputNode.removeTap(onBus: 0)

self.recognitionRequest = nil
self.recognitionTask = nil

self.recordButton.isEnabled = true
self.recordButton.setTitle("Start Recording", for: [])
}
}

最佳答案

这就是最终为我工作的:

func restartSpeechTimer() {
timer?.invalidate()
timer = Timer.scheduledTimer(withTimeInterval: 1.5, repeats: false, block: { (timer) in
// Do whatever needs to be done when the timer expires
})
}

在识别任务中:

var isFinal = false
if letresult = result {
// do something with the result
isFinal = result.isFinal
}
if iFinal {
self.stopRecording()
}
else if error == nil {
self.restartSpeechTimer()
}

关于ios - Swift:在沉默 x 秒后停止语音识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45111072/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com