python - 从另一个函数调用时音频未完成
问题描述
我有以下代码准工作
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”(17 哦六)但是,如果我从 listen() 函数调用它,python 将开始播放文件,但它会被切断,更像是“Se”,然后什么都没有.
我尝试了多种方法,包括使用 time.sleep(n),但是当通过 listen(n) 函数调用时,似乎没有任何更改可以使整个文件播放。
解决方案
推荐阅读
- java - 如何修复“应用程序崩溃并在 mAuth = FirebaseAuth.getInstance(); 上出错”
- python - Pandas fillna() 不适用于 DataFrame 切片
- python - 我怎样才能进入这个类的角度?
- php - One button working, other button gives error of: "Notice: Undefined index: dbconfig.php on line 6"
- python-3.x - Python selenium select option from dropdown
- javascript - 如何将复选框值推送到数组中?
- python - Circular Primes Project Euler #35
- r - 在 R 中绘制时间序列时的日期顺序
- python - 使用 LineairSVM 以整数/双精度值预测情绪得分
- python - 如何在 shell 中模拟给 python 的信号?(不使用 kill)