How to Avoid Errors When Using Swift to Run SFSpeakRecognizer and AVSpeechSynthsizer in One App

Asked 1 years ago, Updated 1 years ago, 105 views

This is a question in Swift 4.2.
When using SFSpeakRecognizer and AVSpeechSynthesizer in one app,
For example, I tried to perform voice recognition with the RecognizeViewController and read aloud with SpeechButton of the SpeechViewController, but I have a problem with the error Thread1:signal SIGABRT, so I have a question.https://qiita.com/croquette0212/items/bf0e41ca1b65c6d320b4
After reading this article, I thought it would be good to use the AVSession setCategory differently.
Voice recognition processing uses the recording processing method

private func startRecording()throws{
    // Describe the action to record here
    iflet recognitionTask = recognitionTask {
        // reset processing
        recognitionTask.cancel()
        self.recognitionTask=nil
        let audioSession=AVAudioSession.sharedInstance()
        try audioSession.setCategory (AVAudioSession.Category.record, mode:.default)
        try audioSession.setMode (AVAudioSession.Mode.measurement)
        tryaudioSession.setActive(true, options: .notifyOthersOnDeactivation)
    }
}

and for SpeechViewController,

vartalker=AVSpeechSynthsizer()

override func viewDidLoad(){
    // speechButton generation process
    speechButton.addTarget(resultCardView, action:#selector(speachButtonTapped(sender:))), for: .touchUpInside 
}

@objc func speachButtonTapped(sender:Any){
    let avSession=AVAudioSession.sharedInstance()
    try?avSession.setCategory(AVAudioSession.Category.ambient, mode: .default, options: .mixWithOthers)
    letutterance=AVSpeechUtterance(string:self.Jplabel.text!)
    utterance.voice = AVSpeechSynthesisVoice (language: "en-En")
    utility.volume=1.2
    // execution
    self.talker.speak (utterance)
}

I wrote it like this, but it turned out to be Thread1:signal SIGABRT.
If anyone understands, please let me know.

swift ios iphone swift4

2022-09-30 21:37

1 Answers

Since there is no var speechButton:UIButton! (! or ?) between the class and func declarations, does the instance stored in speechButton fall out of viewDidLoad(/code>)?

vartalker=AVSpeechSynthsizer()

on the next line of the
var speechButton:UIButton!

If you add speechButton as a member variable of the class and delete the declaration part of speechButton in viewDidLoad() (let in letspeechButton), it will also move to the instance.

One more thing,

utterance.voice=AVSpeechSynthesisVoice (language: "en-En")

The language does not have the code en-EN and
en-US (USA), en-GB (UK), en-AU (Australia), en-IE (Ireland), or en-ZA (South Africa).

Also, regarding setCategory, the argument for option: is [], so

try?avSession.setCategory (AVAudioSession.Category.ambient, mode:.default, options: [.mixWithOthers])

Isn't that right?


2022-09-30 21:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.