ios – The way to clear buffer lead to Speech Recognizer in Swift


I am making an attempt to write down speech recognizer which may have a capability to be cleared after some length like 2 seconds with out switching it off

    personal func startRecording() throws {
        self.recognitionTask = nil
        let audioSession = AVAudioSession.sharedInstance()
        attempt audioSession.setCategory(.document, mode: .measurement, choices: .duckOthers)
        attempt audioSession.setActive(true, choices: .notifyOthersOnDeactivation)
        let inputNode = audioEngine.inputNode

        recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
        guard let recognitionRequest = recognitionRequest else { fatalError("Unable to create a SFSpeechAudioBufferRecognitionRequest object") }
        recognitionRequest.shouldReportPartialResults = true
        // Maintain speech recognition knowledge on machine
        if #accessible(iOS 13, *) {
            recognitionRequest.requiresOnDeviceRecognition = false
        recognitionTask = speechRecognizer.recognitionTask(with: recognitionRequest) { end result, error in
            var isFinal = false
            if let end result = end result {
                self.textView.textual content = end result.bestTranscription.formattedString

                // MARK: right here after 2 seconds my recognition ought to clear it is personal lead to string

                isFinal = end result.isFinal
                print("Textual content (end result.bestTranscription.formattedString)")
            if error != nil || isFinal {
                inputNode.removeTap(onBus: 0)

                self.recognitionRequest = nil
                self.recognitionTask = nil

                self.recordButton.isEnabled = true
                self.recordButton.setTitle("Begin Recording", for: [])
        let recordingFormat = inputNode.outputFormat(forBus: 0)
        inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
        audioEngine.put together()
        attempt audioEngine.begin()
        textView.textual content = "(Go forward, I am listening)"

I cannot rebuild speech recognition, it needs to be work continually.

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

I attempted to do like this however after it my microphone turns off


Leave a Reply

Your email address will not be published. Required fields are marked *