问题
The volume in the view controller with the label has become extremely quiet even when just transferring text from the UITextView to the UILabel.
Almost evrything is working perfectly apart from the volume issue.
Speech Class File:
import UIKit
import AVFoundation
class TextToSpeech {
private let synthesizer = AVSpeechSynthesizer()
var Rate: Float = AVSpeechUtteranceDefaultSpeechRate
var Voice = AVSpeechSynthesisVoice(language: "en-US")
func Say(_ phrase: String) {
let Utterance = AVSpeechUtterance(string: phrase)
Utterance.rate = Rate
Utterance.voice = Voice
synthesizer.speak(Utterance)
}
}
Text To Speech Controller:
import UIKit
import AVFoundation
class TextToSpeechTest: UIViewController {
let speak = TextToSpeech()
let label = UILabel()
override func viewDidLoad(){ super.viewDidLoad()
speak.Say(label.text!)
}
}
Speech To text Controller:
import UIKit
import AVFoundation
import Speech
class SpeechToText: UIViewController {
let textView = UITextView()
let audioEngine = AVAudioEngine()
let speechRecognizer: SFSpeechRecognizer? = SFSpeechRecognizer()
let request = SFSpeechAudioBufferRecognitionRequest()
var recognitionTask: SFSpeechRecognitionTask?
let speechToTextButton = UIButton()
let textToSpeechButton = UIButton()
func recordAndConvertSpeech() {
let node = audioEngine.inputNode
let recordingFormat = node.outputFormat(forBus: 0)
node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) {
buffer, _ in self.request.append(buffer)
}
audioEngine.prepare()
do {
try audioEngine.start()
} catch {
return print(error)
}
guard let myRecognizer = SFSpeechRecognizer()
else { return }
if !myRecognizer.isAvailable { return }
recognitionTask = speechRecognizer?.recognitionTask(with: request, resultHandler: { result, error in
if let result = result {
let bestString = result.bestTranscription.formattedString
self.textView.text = bestString
} else if let error = error {
print(error)
}
})
}
@objc func speechToTextButton() {
recordAndConvertSpeech()
}
@objc func textToSpeechButton() {
let textToSpeechTest = TextToSpeechTest()
self.navigationController?.pushViewController(textToSpeechTest, animated: true)
textToSpeechTest.label.text = textView.text
}
}
Just want the volume to be normal. It was fine before I added the speech recognition.
回答1:
This happened because you cause the mic to be on, when mic is on, the system reduces the speaker to prevent recursive noises.
Some options available when mic is on: - Use earphone speaker - Use handsfree
Because if the speaker continues on it's original loud and clear sound, it will continuously hear itself through microphone.
来源:https://stackoverflow.com/questions/56514413/volume-has-dropped-significantly-in-text-to-speech-since-adding-speech-to-text