问题描述
from gtts import gTTS
import speech_recognition as rs
import pyaudio
import audioop
import os
import math
from os import system
import threading
from datetime import datetime
def say(text):
filename = 'speak.mp3'
language = 'en'
myobj = gTTS(text=text,lang=language,slow=False)
myobj.save(filename)
player = vlc.MediaPlayer(filename)
player.play()
def listen(x):
r=rs.Recognizer()
with rs.Microphone() as source:
audio=r.listen(source)
try:
text = r.recognize_google(audio)
process(text.lower())
except:
system('say I did not get that. Please say again.')
listen(0)
def process(text):
print(text)
# Do things here based on text
if 'what time is it' in text:
say(datetime.Now().strftime("%H%M"))
return
#process("what time is it") "Seventeen oh six"
# Listen for me to say something,if I say 'what time is it' return the time
#listen(0) "Se"
如果我手动运行进程(文本),例如:
process("what time is it")
Python会跟我说类似“ 1706”(十七岁,六岁) 但是,如果我从listen()函数调用它,则python将开始播放文件,但它会被截断,更像是“ Se”,然后什么都没有。
我已经尝试了多种方法,包括使用time.sleep(n),但是通过listen(n)函数调用时似乎没有任何变化可以使整个文件播放。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)