简体   繁体   English

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

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

I'm trying to run Text To Speech (AVSpeechSynthesizer) along with Speech To Text from Siri Kit, but I'm stuck with it. 我正在尝试与Siri Kit中的“ 语音转文本”一起运行“语音文本 (AVSpeechSynthesizer)”,但是我对此一无所知。

My TTS works perfectly until I run the code to execute the STT, after that my TTS doesn't work anymore. 在我运行代码以执行STT之前,我的TTS可以正常工作,之后我的TTS不再起作用。 I debugged the code and during the executing of the code, no errors happen, but my text is not transforming to speech. 我调试了代码,并且在执行代码期间没有发生任何错误,但是我的文本没有转换为语音。 I think somehow my STT is disabling the output microphone and that's why the TTS doesn't transform the text to speech anymore, well, that's just a theory. 我想我的STT会以某种方式禁用输出麦克风,这就是为什么TTS不再将文本转换为语音的原因,那只是一个理论。 Ops: My TTS stops working, but my STT works perfectly 操作:我的TTS停止工作,但我的STT正常运行

Any tips? 有小费吗?

Here's my viewController's code: 这是我的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!"
}

} }

Probably because your audiosession is in Record mode, You have 2 solutions, first would be to set your try audioSession.setCategory(AVAudioSessionCategoryRecord) to AVAudioSessionCategoryPlayAndRecord (This will work) but a cleaner way would be to get a separate function for saying something and then set your AVAudioSessionCategory to AVAudioSessionCategoryPlayback 可能是因为您的音频会话处于“录制”模式,所以您有2种解决方案,首先是将try audioSession.setCategory(AVAudioSessionCategoryRecord)设置为AVAudioSessionCategoryPlayAndRecord(这将起作用),但是一种更简洁的方法是获得一个单独的函数来声明内容,然后将您的AVAudioSessionCategory设置为AVAudioSessionCategoryPlayback

Hope this helped. 希望这会有所帮助。

This line: 这行:

try audioSession.setMode(AVAudioSessionModeMeasurement)

is probably the reason. 可能是原因。 It can cause the volume to be throttled so low, that it sounds like it is off. 可能会导致音量调得太低,听起来好像已经关闭。 Try: 尝试:

try audioSession.setMode(AVAudioSessionModeDefault)

and see if it works. 看看是否可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM