so i am writing a code that converts text to speech and save it as output.mp3 and as i enter next text it shows this error
with metadata, open(audio_fname, "wb") as audio:
~~~~^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: 'output.mp3'
so i tried to first play the audio first and wait for 5 sec and then delete but still i am getting error.
ah the folder structureis like this
main
text.py
converter.py
output.mp3
well the audio is being played but after the audio stops and when the code tries to delete it it shows that error
full code
class Speak:
def __init__(self, text):
self.text = str(text)
async def text_to_speech(self, voice='en-US-JennyNeural'):
# Initialize Edge TTS engine
communicate = edge_tts.Communicate(self.text, voice)
# Save the speech as an MP3 file
await communicate.save("output.mp3")
# Play the MP3 file in a separate thread to avoid blocking
def play_music():
pygame.mixer.music.load("output.mp3")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
try:
os.remove("output.mp3")
print("File 'output' deleted successfully.")
except Exception as e:
print(f"Error deleting file: {e}")
# Start the playing music in a separate thread
music_thread = threading.Thread(target=play_music)
music_thread.start()
# Function to start the voice initiation
async def initiation_voice(text):
text_instance = Speak(text=text)
await text_instance.text_to_speech()
The Error messages :
Hello from the pygame community. .html
Enter your message: hello
Hello there! How can I help you today?
Enter your message: Error deleting file: [WinError 32] The process cannot access the file because it is being used by another process: 'output.mp3'
here comes the errors
Traceback (most recent call last):
File "c:\Users\kcmr9\Vera\main\main.py", line 11, in <module>
brain_instance.model()
~~~~~~~~~~~~~~~~~~~~^^
File "c:\Users\kcmr9\Vera\main\brain.py", line 86, in model
asyncio.run(initiation_voice(response.text))
~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\kcmr9\AppData\Local\Programs\Python\Python313\Lib\asyncio\runners.py", line 195, in run
return runner.run(main)
~~~~~~~~~~^^^^^^
File "C:\Users\kcmr9\AppData\Local\Programs\Python\Python313\Lib\asyncio\runners.py", line 118, in run
return self._loop.run_until_complete(task)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
File "C:\Users\kcmr9\AppData\Local\Programs\Python\Python313\Lib\asyncio\base_events.py", line 725, in run_until_complete
return future.result()
~~~~~~~~~~~~~^^
File "c:\Users\kcmr9\Vera\main\brain.py", line 8, in initiation_voice
await text_instance.text_to_speech() # Calling the async method correctly
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\kcmr9\Vera\main\Speak.py", line 22, in text_to_speech
await communicate.save("output.mp3")
File "C:\Users\kcmr9\AppData\Local\Programs\Python\Python313\Lib\site-packages\edge_tts\communicate.py", line 506, in save
with metadata, open(audio_fname, "wb") as audio:
~~~~^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: 'output.mp3'