Siri Kit(语音转文本)禁用我的 TTS(文本转语音)iOS

Siri Kit (Speech to text) disabling my TTS (Text to speech) iOS

我正在尝试 运行 Text To Speech (AVSpeechSynthesizer) 以及来自 Siri Kit 的 Speech To Text,但我坚持了下来。

我的 TTS 工作完美,直到我 运行 执行 STT 的代码,之后我的 TTS 就不再工作了。我调试了代码,在代码执行期间没有发生错误,但我的文本没有转换为语音。我认为我的 STT 以某种方式禁用了输出麦克风,这就是 TTS 不再将文本转换为语音的原因,好吧,这只是一种理论。 Ops: 我的 TTS 停止工作,但我的 STT 工作正常

有什么建议吗?

这是我的 viewController 代码:

@IBOutlet weak var microphoneButton: UIButton!

//text to speech
let speechSynthesizer = AVSpeechSynthesizer()

//speech to text
private var speechRecognizer: SFSpeechRecognizer!

private var recognitionRequest: SFSpeechAudioBufferRecognitionRequest?
private var recognitionTask: SFSpeechRecognitionTask?
private var audioEngine = AVAudioEngine()

@IBAction func textToSpeech(_ sender: Any) {

    if let word = wordTextField.text{

        if !speechSynthesizer.isSpeaking {


            //get current dictionary
            let dictionary = fetchSelectedDictionary()

            //get current language
            let language = languagesWithCodes[(dictionary?.language)!]

            let speechUtterance = AVSpeechUtterance(string: word)
                speechUtterance.voice = AVSpeechSynthesisVoice(language: language)
                speechUtterance.rate = 0.4
             //speechUtterance.pitchMultiplier = pitch
             //speechUtterance.volume = volume
                speechSynthesizer.speak(speechUtterance)

        }
        else{
            speechSynthesizer.continueSpeaking()
        }

    }
}

@IBAction func speechToText(_ sender: Any) {

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

}

func startRecording() {

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

    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(AVAudioSessionCategoryRecord)
        try audioSession.setMode(AVAudioSessionModeMeasurement)
        try audioSession.setActive(true, with: .notifyOthersOnDeactivation)
    } catch {
        print("audioSession properties weren't set because of an error.")
    }

    recognitionRequest = SFSpeechAudioBufferRecognitionRequest()

    guard let inputNode = audioEngine.inputNode else {
        fatalError("Audio engine has no input node")
    }

    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.wordTextField.text = result?.bestTranscription.formattedString
            isFinal = (result?.isFinal)!
        }

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

            self.recognitionRequest = nil
            self.recognitionTask = nil

            self.microphoneButton.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.")
    }

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

}

可能是因为您的音频会话处于录制模式,您有 2 个解决方案,首先是将您的尝试 audioSession.setCategory(AVAudioSessionCategoryRecord) 设置为 AVAudioSessionCategoryPlayAndRecord(这会起作用),但更简洁的方法是获得一个用于说某事的单独函数,然后将 AVAudioSessionCategory 设置为 AVAudioSessionCategoryPlayback

希望对您有所帮助。

这一行:

try audioSession.setMode(AVAudioSessionModeMeasurement)

大概是这个原因。它可能会导致音量被限制得如此之低,以至于听起来像是关闭了。尝试:

try audioSession.setMode(AVAudioSessionModeDefault)

看看它是否有效。