3

So I've been working on a mini-JARVIS sort of thing in python and I've made a lot of progress. What happens is that when I run my code, first it stores the AI speech into an mp3 file, and then plays that mp3 using pygame. However, when I try to delete the mp3 file, it says that "the process cannot access the file because it is being used by another process" then the file name. What confuses me even more is that I used this .py to actually make the mp3, and I'm using the same one to try to delete it, yet it's saying it's being used by another process? How will I be able to delete the files, because even after using task manager and making sure no python files are running, it still says its being used by another process, and restarting the computer doesn't work either. I could use any help, thanks. Here is my code.

#google text to speech, for putting into mp3
from gtts import gTTS
#actual speech recognition library
import speech_recognition as speech_recog
#to play the mp3
from pygame import mixer
#to get the time and the date
import time
#to print the calendar
import calendar
#to delete the file after it has been played
import os

#make the computer talk
def speech_output(ai_string):
    tts = gTTS(text=ai_string, lang='en-us')
    comp_string = str('compspeech.mp3')
    tts.save(comp_string)

    mixer.init()
    mixer.music.load(comp_string)
    mixer.music.play()
    print('AI Speech:', ai_string)
    os.remove('compspeech.mp3')

#get user's speech
def speech_input():
    r = speech_recog.Recognizer()
    with speech_recog.Microphone() as source:
        print('You may speak after AI has talked ')
        user_speech = r.listen(source)

    try:
        print("Sphinx thinks you said: " + r.recognize_sphinx(user_speech))
    except sr.UnknownValueError:
        print("Sphinx could not understand audio")
    except sr.RequestError as e:
        print("Sphinx error - {0}".format(e))

    #start testing for what user said
    if user_speech == 'what is the time' or user_speech == 'what is the date':
        time(user_speech)
    elif user_speech == 'show me a calendar':
        show_calendar()
    else:
        speech_output('No action found for that statement, sorry about that.')

#get the date and the time
def date_time():
    current_date_time = time.asctime()
    speech_output(current_date_time)

#display the calendar
def show_calendar():
    speech_output('Please enter the year for the calendar you want to see')
    year = int(input('Type year here: '))
    print(calendar.calendar(year, 2, 1, 10))

speech_output('Hi')
speech_input()

The last 2 lines are just to execute the basic AI and user talking to make sure they actually work. The error happens in the speech_output function, on "os.remove()", as you probably guessed.

1 Answers1

2

mixer.music.play() starts playing in background, so when you are trying to delete the file it is still playing. You need to wait for the sound to end, something like

mixer.init()
mixer.music.load(comp_string)
mixer.music.play()
while pygame.mixer.music.get_busy(): 
    pygame.time.Clock().tick(10)
os.remove(mp3file)

See for details how play mp3 with pygame

Nikolay Shmyrev
  • 24,897
  • 5
  • 43
  • 87
  • This doesn't work for me. Even if I use `time.sleep()` way past the duration of the music file, `os.remove()` will raise a `PermissionError` saying another process is using the file. And that's because the file is still loaded in the mixer. As soon as I load another file, I can delete the previous one. – Reti43 Feb 16 '18 at 23:20