正在删除语音助手上的后端mp3文件



我正在使用教程构建一个语音助手。我附上下面的链接供参考

现在,在函数def-assistant_sports(output(中,我做了一些更改。现在,每当助理讲话时,都会生成一个后端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)

这对我很有效。我没有将变量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)

希望这对你有用。(如果没有,请告诉我(

最新更新