On Linux, the following example works fine:
import pygame
from gtts import gTTS
pygame.mixer.init()
t = 'temp.mp3'
for i in range(5):
gTTS(str(i)).save(t)
pygame.mixer.music.load(t)
pygame.mixer.munic.play()
This will play the numbers 0-4.
On Windows, the same code will cause the error:
PermissionError: [Errno 13] Permission denied: 'temp3.mp3'
when i==1
. That is, it crashes on the second loop. The crash is inside of gTTS.save
at:
with open(savefile, 'wb') as f:
self.write_to_fp(f)
As a work around, I've also tried:
with tempfile.TemporaryFile() as t:
gTTS(str(i)).save(t)
pygame.mixer.music.load(t)
pygame.mixer.munic.play()
but get the error
error: Couldn't read from RWops
It's also worth noting that this is a minimal example that fails every time. In my full code, I usually write and play infrequently, which is successful, and I can play from temp.py
multiple times in the same script. The issue appears to occur only if I attempt to write and read back-to-back multiple times.