I am trying to implement a speech-to-text feature in my app but I am facing this error:
Recognition error: Error Domain=kAFAssistantErrorDomain Code=1110 "No speech detected"
I am testing it on a real device
Relevant code:
@State private var isRecording = false {
didSet {
if !isRecording { recognitionTask = nil }
}
}
@State private var recognitionTask: SFSpeechRecognitionTask?
private func startRecording() {
isRecording = true
let speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: "en-US"))!
// Create an SFSpeechAudioBufferRecognitionRequest instance
let request = SFSpeechAudioBufferRecognitionRequest()
let audioEngine = AVAudioEngine()
let inputNode = audioEngine.inputNode
// Append audio samples to the request object
inputNode.installTap(onBus: 0, bufferSize: 1024, format: inputNode.outputFormat(forBus: 0)) { (buffer, time) in
request.append(buffer)
}
// Start the audio engine and start recording
audioEngine.prepare()
do {
try audioEngine.start()
} catch {
print("Audio engine error: ",error)
return
}
// Start recognizing speech from the audio samples
recognitionTask = speechRecognizer.recognitionTask(with: request) { (result, error) in
// Check for errors
if let error {
print("Recognition error: ",error)
return
}
guard let result else { return }
// Update the task variable with the transcription result
self.task = result.bestTranscription.formattedString
}
}
private func stopRecording() {
recognitionTask?.cancel()
isRecording = false
}
and then I have 2 buttons to indicate if it's recording or not and also to trigger the stopRecording()
function and the startRecording()
function.
Here's the snippet:
if isRecording {
Button {
stopRecording()
} label: {
Image(systemName: "mic.circle.fill")
.resizable()
.frame(width: 25, height: 25)
.foregroundColor(.red)
}
} else {
Button {
startRecording()
} label: {
Image(systemName: "mic.circle.fill")
.resizable()
.frame(width: 25, height: 25)
}
}
Why do I get this error?