我正在使用教程构建一个语音助手。I am attaching the link below for reference
现在,在函数def assistant_speaks(输出)中,我做了一些更改。现在,每当助手说话时,都会生成一个后端mp3文件。这些文件是随机编号的。我想在语音助手停止后删除这些mp3文件。如何做到这一点呢?这是代码-
num = random.randint(1,10000000000)
def assistant_speaks(output):
global num
num += 1
print("PerSon : ", output)
toSpeak = gTTS(text = output, lang ='en', slow = False)
file = str(num)+".mp3"
toSpeak.save(file)
playsound.playsound(file, True)
os.remove(file)发布于 2021-02-13 18:22:26
这对我很有效。我没有使用变量num作为全局变量,而是直接将其传递给函数。
def assistant_speaks(output, num):
num += 1
print("PerSon : ", output)
toSpeak = gTTS(text = output, lang ='en', slow = False)
file = str(num)+".mp3"
toSpeak.save(file)
playsound.playsound(file, True)
os.remove(file)
num = random.randint(1,10000000000)
assistant_speaks("Hello", num)希望这对你有用。(如果没有,请让我知道)
https://stackoverflow.com/questions/65128622
复制相似问题