How to free resource in PyGame mixer?

99封情书 提交于 2019-12-08 04:28:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!