具有语音识别功能的Xcode Project只能在一台计算机上运行,​​而不能在其他计算机上运行

时间:2019-07-12 23:47:04

标签: ios swift speech-recognition

我正在从事一个涉及ios应用语音识别的项目,并将在屏幕上说出的所有内容打印出来。我正在使用语音库,并且可以在我的计算机上使用它,但是当我的同事在他的计算机中打开该项目时,该程序可以编译,但是语音识别无法打印任何内容。有谁知道为什么会这样?

我已经尝试解决这个问题了好几个小时,但无法在网上找到任何有关它的信息。我也尝试过在他的计算机上重新安装Xcode,但是也没有用。

这是我的代码:

import UIKit
import Speech

class ViewController: UIViewController {
    @IBOutlet weak var activateBtn: UIButton!

    @IBOutlet weak var textView: UITextView!
    private var speechRecognizer = SFSpeechRecognizer(locale:Locale.init(identifier:"en-Us"))//1
    private var recognitionRequest: SFSpeechAudioBufferRecognitionRequest?
    private var recognitionTask: SFSpeechRecognitionTask?
    private var audioEngine = AVAudioEngine()
    var lang:String = "en-US"

    override func viewDidLoad() {
        super.viewDidLoad()

        activateBtn.isEnabled = false //2
        speechRecognizer?.delegate = self as? SFSpeechRecognizerDelegate//3
        speechRecognizer = SFSpeechRecognizer(locale:Locale.init(identifier: lang))
        SFSpeechRecognizer.requestAuthorization{ (authStatus) in //4
            var isButtonEnabled = false

            switch authStatus{//5
            case .authorized:
                isButtonEnabled = true
            case .denied:
                isButtonEnabled = false
                print("User denied access to speech recognition")
            case .restricted:
                isButtonEnabled = false
                print("Speech recognition restricted on this device")
            case .notDetermined:
                isButtonEnabled = false
                print("Speech recognition not yet authorized")
            }
            OperationQueue.main.addOperation(){
                self.activateBtn.isEnabled = isButtonEnabled
            }
        }
    }

    @IBAction func activateMic(_ sender: Any) {
        print("activateMic")
        speechRecognizer = SFSpeechRecognizer(locale:Locale.init(identifier:lang))

        if audioEngine.isRunning{
            audioEngine.stop()
            recognitionRequest?.endAudio()
            activateBtn.isEnabled = false
            activateBtn.setTitle("Start Recording", for: .normal)
        }else{
            startRecording()
            activateBtn.setTitle("Stop Recording", for: .normal)
        }
    }

    func startRecording() {
        print("Start Recording")

        if recognitionTask != nil {
            recognitionTask?.cancel()
            recognitionTask = nil
        }

        let audioSession = AVAudioSession.sharedInstance()
        do {
            try audioSession.setCategory(AVAudioSession.Category.record)
            try audioSession.setMode(AVAudioSession.Mode.measurement)
            try audioSession.setActive(true)
        } catch {
            print("audioSession properties weren't set because of an error.")
        }

        recognitionRequest = SFSpeechAudioBufferRecognitionRequest()

        let inputNode = audioEngine.inputNode

        guard let recognitionRequest = recognitionRequest else {
            fatalError("Unable to create an SFSpeechAudioBufferRecognitionRequest object")
        }

        recognitionRequest.shouldReportPartialResults = true

        recognitionTask = speechRecognizer?.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in

            var isFinal = false

            if result != nil {

                self.textView?.text = result?.bestTranscription.formattedString

                //Added stop after the word "stop" is part of the string
                if (((result?.bestTranscription.formattedString)?.contains("stop"))!) {
                    self.audioEngine.stop()
                }

                isFinal = (result?.isFinal)!
            }

            if error != nil || isFinal {
                self.audioEngine.stop()
                inputNode.removeTap(onBus: 0)

                self.recognitionRequest = nil
                self.recognitionTask = nil

                self.activateBtn.isEnabled = true
            }
        })

        let recordingFormat = inputNode.outputFormat(forBus: 0)
        inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, when) in
            self.recognitionRequest?.append(buffer)
        }

        audioEngine.prepare()

        do {
            try audioEngine.start()
        } catch {
            print("audioEngine couldn't start because of an error.")
        }

        textView?.text = "Say something, I'm listening!"
    }

    func speechRecognizer(_ speechRecognizer: SFSpeechRecognizer, availabilityDidChange available: Bool) {
        print("Speech Recognizer")
        if available {
            activateBtn.isEnabled = true
        } else {
            activateBtn.isEnabled = false
        }
    }

}

1 个答案:

答案 0 :(得分:0)

您问题中的关键字可能是“计算机”。语音识别在模拟器中不一定有效。您的朋友需要在设备上运行您的代码。