如何将完成的笔录字符串从Google语音返回到文本回复?

时间:2018-06-24 19:45:35

标签: swift google-speech-api google-text-to-speech

func processSampleData(_ data: Data) -> Void {
    audioData.append(data)

    // We recommend sending samples in 100ms chunks
    let chunkSize : Int /* bytes/chunk */ = Int(0.1 /* seconds/chunk */
      * Double(SAMPLE_RATE) /* samples/second */
      * 2 /* bytes/sample */);

    if (audioData.length > chunkSize) {
      SpeechRecognitionService.sharedInstance.streamAudioData(audioData,
                                                              completion:
        { [weak self] (response, error) in
            guard let strongSelf = self else {
                return
            }

            var finalResult = StreamingRecognitionResult()

            if let error = error {
                strongSelf.textView.text = error.localizedDescription
            } else if let response = response {
                var finished = false
                //print(response)
                for result in response.resultsArray! {
                    if let result = result as? StreamingRecognitionResult {
                        if result.isFinal {
                            finished = true
                            finalResult = result
                        }
                    }
                }
                //let textToView = response.description
                let textToView = finalResult.alternativesArray[0]
                strongSelf.textView.text = textToView as! String
                if finished {
                    strongSelf.stopAudio(strongSelf)
                }
            }
      })
      self.audioData = NSMutableData()
    }
}

“ textToView”应仅是用户语音的最后一个字符串,但是我似乎无法弄清楚如何真正从Google云响应中提取此字符串,并且仅从字符串中提取该字符串。

对于任何使用过Google语音到文本API语音处理的人,我都非常感谢您的答复,因为这已经困扰了我很长时间了。

0 个答案:

没有答案