I have a python speech recognition assistant and it plays mp3 audios it downloads. I have put playing the mp3 on a separate thread in the background.
The issue is that the speech recognition tries to detect what the mps audio is saying and it responds.
How can I make the speech recognition be silent until I give specific speech to wake it up?
Here is my function file for playing and retrieving the mp3:
def play_quran():
speak("Ready to play Quran. Tell me which Surah number you want to hear.")
#qari_num = input("Enter Surah Number: ")
qari_num = recordAudio()
url = ("https://api.quran.com/api/v4/chapter_recitations/9/" + str(qari_num))
print(url)
response = requests.get(url)
my_dictionary = requests.get(url).json()
rdata = response.json()
print(json.dumps(my_dictionary, indent=4))
surah_to_play = (my_dictionary['audio_file']['audio_url'])
print(surah_to_play)
response = request.urlretrieve(surah_to_play, qari_num + ".mp3")
os.system("mpg123 -q " + qari_num + ".mp3")
stop_listening = sr.Recognizer().listen_in_background(sr.Microphone(), recordAudio)
# time.sleep(2)
# exit()
Here is the code that calls the function above:
if "play Quran" in data:
speak("opening Quran. One moment please")
t = threading.Thread(
target=play_quran) # < Note that I did not actually call the function, but instead sent it as a parameter
t.daemon = True
t.start() # < This actually starts the thread execution in the background
Thanks.