ios - Clear input of SFSpeechAudioBufferRecognitionRequest after every result (Swift3) -
i have integrated speech text through appcoda tutorial. problem facing want user can write/edit himself, sfspeechaudiobufferrecognitionrequest doesn't take consideration thing user has typed.
what's way send user typed input in sfspeechaudiobufferrecognitionrequest or way clear sfspeechaudiobufferrecognitionrequest input params before sending new request.
thanks in advance.
here use create recognition request:
func recordspeech() throws { // cancel previous task if it's running. if let recognitiontask = recognitiontask { recognitiontask.cancel() self.recognitiontask = nil } isrecognizing = true self.delegate?.recognitionstarted(sender: self) let audiosession = avaudiosession.sharedinstance() try audiosession.setcategory(avaudiosessioncategoryrecord) try audiosession.setmode(avaudiosessionmodemeasurement) try audiosession.setactive(true, with: .notifyothersondeactivation) recognitionrequest = sfspeechaudiobufferrecognitionrequest() guard let inputnode = audioengine.inputnode else { print("there error in audioengine.inputnode") fatalerror("audio engine has no input node") } guard let recognitionrequest = recognitionrequest else { fatalerror("unable create sfspeechaudiobufferrecognitionrequest object") } // configure request results returned before audio recording finished recognitionrequest.shouldreportpartialresults = true // recognition task represents speech recognition session. // keep reference task can cancelled. recognitiontask = recognizer.recognitiontask(with: recognitionrequest) { result, error in func finalizeresult() { self.audioengine.stop() inputnode.removetap(onbus: 0) self.recognitionrequest = nil self.recognitiontask = nil } guard error == nil else { finalizeresult() return } if !(result?.isfinal)! { guard self.isrecognizing else { return } // process partial result self.processrecognition(result: result) } else { finalizeresult() } } let recordingformat = inputnode.outputformat(forbus: 0) inputnode.installtap(onbus: 0, buffersize: 1024, format: recordingformat) { (buffer, when) in self.recognitionrequest?.append(buffer) } audioengine.prepare() { try audioengine.start() } catch let error nserror { print("audio engine start error=\(error)") } }
to cancel or stop @ point use these methods:
@objc func stoprecording() { isrecognizing = false audioengine.stop() recognitionrequest?.endaudio() self.delegate?.recognitionfinished() } func cancelrecording() { isrecognizing = false audioengine.stop() recognitiontask?.cancel() self.delegate?.recognitionfinished() }
i setup button trigger speech recognition , tie recordspeech()
. setup button , tie stoprecording()
. when user stops request, result?.isfinal
true , know final text first input. user use speech input again second set of speech.
most of code came 2016 wwdc session on speech recognition can find here:
Comments
Post a Comment