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.