问题
I use gTTS python module to get mp3 from Google Text-To-Speech API and PyGame to play output mp3 files without opening external player (is there any simpler way to do it?)
However it seems like PyGame mixer doesn't free file resource, even after it's quit method.
phrase = "Hello!"
tts = gtts.gTTS(text=phrase, lang='en')
tts.save("googleTTS.mp3")
f = MP3("googleTTS.mp3")
mixer.init(f.info.sample_rate)
mixer.music.load("googleTTS.mp3")
mixer.music.play()
while mixer.music.get_busy() == True:
continue
mixer.quit() # doesn't free resource?
phrase = "Bye!"
tts = gtts.gTTS(text=phrase, lang='en')
tts.save("googleTTS.mp3")
Last line gives exception:
IOError: [Errno 13] Permission denied: 'googleTTS.mp3'
I should notice that the problem isn't in tts.save function, cause code without mixer works fine.
How can I free mixer resource and use the same file over and over again?
回答1:
This way works for me:
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
clock.tick(30)
pygame.mixer.music.stop()
pygame.mixer.quit()
来源:https://stackoverflow.com/questions/31809050/how-to-free-resource-in-pygame-mixer