'unable to use speech recognition in python

I am trying to make a virtual assistant for which I need the speech recognition module but it's showing a timeout error I tried to increase the timeout manually to 5 seconds yet the issue prevails.

import speech_recognition as sr
import pyaudio 
import sounddevice as sd
a = sd.query_devices()
print (a)
r = sr.Recognizer()

with sr.Microphone(device_index=2) as source:
    print('say....')
    audio = r.listen(source)
    voice_data = r.recognize_google(audio)
    print(voice_data)

The error shown is as follows

 Traceback (most recent call last):
  File "/Users/vipulvasant/Desktop/my codes/ABP/main.py", line 10, in <module>
    audio = r.listen(source)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/speech_recognition/_init_.py", line 618, in listen
    raise WaitTimeoutError("listening timed out while waiting for phrase to start")
speech_recognition.WaitTimeoutError: listening timed out while waiting for phrase to start

this is the first step towards my dream of making my own virtual assistant



Solution 1:[1]

Adding a simple a while True: statement should fix the issue. As i ran into the same issue and this was my solution.

import speech_recognition as sr
import pyaudio 
import sounddevice as sd
a = sd.query_devices()
print (a)
r = sr.Recognizer()

while True:
    with sr.Microphone(device_index=2) as source:
        print('say....')
        audio = r.listen(source)
        voice_data = r.recognize_google(audio)
        print(voice_data)

Solution 2:[2]

Here try this. I don't know if this is what you are asking for but I hope it helps:

import speech_recognition
r = sr.Recognizer()
with sr.Microphone() as source:
    print("Speak:")
    audio = r.listen(source)
voice_data = r.recognize_google(audio)
print(voice_data)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 General Grievance