我有一个应用程序,我们需要打开基于语音命令的某些屏幕,如果用户说"打开设置"然后它应该打开设置屏幕,到目前为止,我已经使用了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 …
Run Code Online (Sandbox Code Playgroud) 我尝试导入 watchOS 的 SpeechKit 框架并收到错误。有什么办法可以和手表一起使用吗?当我导入 Speechkit 框架时出现错误,提示“没有这样的模块语音”
import WatchKit
import Foundation
import Speech
class SpeechInterfaceController: WKInterfaceController, SFSpeechRecognizerDelegate {
override func awake(withContext context: Any?) {
super.awake(withContext: context)
// Configure interface objects here.
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
Run Code Online (Sandbox Code Playgroud)
}