如何在 PyGame 混合器中释放资源? [英] How to free resource in PyGame mixer?

查看:86
本文介绍了如何在 PyGame 混合器中释放资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 gTTS python 模块从 Google Text-To-Speech API 和 PyGame 获取 mp3 以播放输出 mp3 文件,而无需打开外部播放器(有没有更简单的方法?)

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?)

但是看起来 PyGame 混合器并没有释放文件资源,即使它是退出方法.

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")

最后一行给出异常:

    IOError: [Errno 13] Permission denied: 'googleTTS.mp3'

我应该注意到问题不在 tts.save 函数中,因为没有混合器的代码可以正常工作.

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?

推荐答案

这种方式对我有用:

pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
    clock.tick(30)
pygame.mixer.music.stop()
pygame.mixer.quit()

这篇关于如何在 PyGame 混合器中释放资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆