Python 从另一个函数调用时,音频未完成

Python 从另一个函数调用时,音频未完成,python,python-3.x,media-player,vlc,Python,Python 3.x,Media Player,Vlc,我有以下代码正在工作 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'

我有以下代码正在工作

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(“现在几点了”)

Python会对我说类似于“1706”(17-oh-6)的话 但是,如果我从listen()函数调用它,python将开始播放该文件,但它会被切断,更像是“Se”,然后什么都没有


我尝试过多种方法,包括使用time.sleep(n),但在通过listen(n)函数调用时,似乎没有任何更改可以使整个文件播放。

hack/workaround:它似乎播放了任何字符串的一部分,因此目前我让它说“1706是时间”,实际上它最终播放的是“十七零六”破解/解决方法:它似乎播放了任何字符串的一部分,因此目前我用它说“1706是时候了”,实际上它最后播放的是“十七哦六”