如何停止语音识别器,获取结果然后迅速正确重启

时间:2019-06-25 15:51:13

标签: swift speech-recognition speech-to-text xcode10 sfspeechrecognizer

我已经在我的ios应用中成功实现了SFSpeechRecognizer。现在我想用一个按钮停止识别器并重新启动它。

   if(engine.isRunning){
        mainbutton.setTitle("start", for: .normal)
        print("iwasrunning")
        engine.stop()
        request.endAudio()
        let inputNode = engine.inputNode
        let bus = 0
        inputNode.removeTap(onBus: bus)
        self.engine.stop()

    }else{

    // Install tap to audio engine input node
    mainbutton.setTitle("stop", for: .normal)
    print("engine starting...")
    // Prepare and start audio engine
    engine.prepare()

    do {
        try engine.start()
    } catch {
        return print(error)
    }
 }

使用当前代码,我只能在应用程序启动时第一次获得结果,而下次SFSpeechRecognizer无法启动时。

这是完整的代码,以获取更多详细信息。

import UIKit
import Speech


class ViewController: UIViewController {
@IBOutlet var buttonstart:UIButton!
@IBOutlet var speechtext:UILabel!
@IBOutlet weak var mainbutton: UIButton!


private var recognizer = SFSpeechRecognizer(locale: Locale(identifier: "en-US"))
private var request = SFSpeechAudioBufferRecognitionRequest()
private let engine = AVAudioEngine()

override func viewDidLoad() {
    super.viewDidLoad()

    requestPermissions()
}

@IBAction func startEngine(){

    if(engine.isRunning){
        mainbutton.setTitle("start", for: .normal)

        print("iwasrunning")
        engine.stop()
        request.endAudio()
        let inputNode = engine.inputNode
        let bus = 0
        inputNode.removeTap(onBus: bus)
        self.engine.stop()



    }else{

    // Install tap to audio engine input node
        mainbutton.setTitle("stop", for: .normal)

    print("engine starting...")
    // Prepare and start audio engine
    engine.prepare()

    do {
        try engine.start()
    } catch {
        return print(error)
    }
    }
}

private func requestPermissions() {
    // Request recording permission
    AVAudioSession.sharedInstance().requestRecordPermission { allowed in
        if allowed {
            // Request speech recognition authorization
            SFSpeechRecognizer.requestAuthorization { status in
                switch status {
                case .authorized: self.prepareSpeechRecognition()
                case .notDetermined, .denied, .restricted: print("SFSpeechRecognizer authorization status: \(status).")
                }
            }

        } else {
            print("AVAudioSession record permission: \(allowed).")
        }
    }
}

private func prepareSpeechRecognition() {
    // Check if recognizer is available (has failable initializer)
    guard let recognizer = recognizer else {
        print("SFSpeechRecognizer not supported.")
        return
    }

    // Prepare recognition task
    recognizer.recognitionTask(with: request) { (result, error) in
        var isFinal = false

        if let result = result {
                isFinal = result.isFinal
            if(isFinal){
                self.mainbutton.setTitle("start", for: .normal)

                print("SFSpeechRecognizer result: \(result.bestTranscription.formattedString)")

            }
        } else {
            print("SFSpeechRecognizer error: \(String(describing: error))")
        }
    }

    let inputNode = engine.inputNode
    let busNumber = 0
    let recordingFormat = inputNode.outputFormat(forBus: busNumber)
    inputNode.installTap(onBus: busNumber, bufferSize: 1, format: recordingFormat) {
        buffer,
        time
        in
        self.request.append(buffer);
    }
}
}

0 个答案:

没有答案