AVSpeechsynthesizer在swift

时间:2017-04-10 12:12:54

标签: ios swift speech-recognition dialogflow avspeechsynthesizer

目前我正在开发一个演示应用程序,并且正在分为3个部分 1.演讲到文字(完成) 2.将文本发送到服务器并获取响应(使用API​​.ai)(完成) 3.文本到语音,转换为语音的响应消息不起作用。

文本到语音不在函数内部工作,我可以为其添加更多优先级。

这是代码

@IBAction func startActionTapped(_ sender: Any) {

    if audioEngine.isRunning {
        audioEngine.stop()
        recognitionRequest?.endAudio()
        startButton.isEnabled = false
        startButton.setTitle("Start", for: .normal)

    }

    else {
        startRecording()
        startButton.setTitle("Stop", for: .normal)

        let loadingNotification = MBProgressHUD.showAdded(to: self.view, animated: true)
        loadingNotification.mode = MBProgressHUDMode.indeterminate
        loadingNotification.label.text = "Voice Recogninsing...."

        DispatchQueue.main.asyncAfter(deadline: .now() + 10.0, execute: {

            loadingNotification.label.text = "sending request to server...."


            let request = ApiAI.shared().textRequest()
            request?.query = ["turn on blue led"]

            request?.setMappedCompletionBlockSuccess({ (request, response) in
                let response = response as! AIResponse

                if response.result.action == "light.led" {

                    if let parameters = response.result.parameters as? [String: AIResponseParameter] {
                        if let led = parameters["led"]?.stringValue {

                            switch led {
                            case "red":
                                print("color is red")
                            case "blue":
                                print("color is blue")
                            case "green":
                                print("color is green")
                            default:
                                print("color is :",led)
                            }
                            self.speechToText = ""
                        }
                    }
                } else {
                    print("Invalid LED Color")
                }

                if let textResponse = response.result.fulfillment.speech {
                print(textResponse)
                loadingNotification.hide(animated: true)
                    DispatchQueue.global(qos: .userInitiated).async {

                        let synth = AVSpeechSynthesizer()
                        let utterance = AVSpeechUtterance(string: textResponse)
                        utterance.rate = AVSpeechUtteranceDefaultSpeechRate
                        let lang = "en-US"

                        utterance.voice = AVSpeechSynthesisVoice(language: lang)
                        synth.speak(utterance)

                    }
                }  
            }, failure: { (request, error) in
                print(error!)
            })
            ApiAI.shared().enqueue(request)
        })
    }
    speechToText = ""
}

不知道为什么文字到语音工作。 我的问题是文本到语音在app中不起作用,我是否缺少任何步骤?

let synth = AVSpeechSynthesizer()
                    let utterance = AVSpeechUtterance(string: textResponse)
                    utterance.rate = AVSpeechUtteranceDefaultSpeechRate
                    let lang = "en-US"

                    utterance.voice = AVSpeechSynthesisVoice(language: lang)
                    synth.speak(utterance)

1 个答案:

答案 0 :(得分:1)

更新TTS区域中的代码

{{1}}

需要再次启动音频引擎。