Deleting back-end mp3 files on voice assistant

82 views Asked by At

I am building a voice assistant using a tutorial.I am attaching the link below for reference

Now in the function def assistant_speaks(output) I have made some changes. Now whenever the assistant speaks, a back-end mp3 file is generated. The files are numbered randomly. I want to delete those mp3 files once the voice assistant stops. How to achieve that ? Here is the code -

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)
1

There are 1 answers

0
Cerabbite On

This worked for me. Instead of using the variable num as a global I passed it in directely into the function.

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)

Hope this worked for you. (If not please let me know)