【发布时间】:2015-08-04 12:13:02
【问题描述】:
我使用 gTTS python 模块从 Google Text-To-Speech API 和 PyGame 获取 mp3 以播放输出 mp3 文件而无需打开外部播放器(有没有更简单的方法?)
但是看起来 PyGame 混合器并没有释放文件资源,即使在它的退出方法之后。
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 函数中,因为没有混音器的代码可以正常工作。
如何释放混音器资源并反复使用同一个文件?
【问题讨论】:
-
也许问题出在
f?你试过del f什么的吗?不知道MP3class(?) 做了什么。 -
不,当我使用像mixer.init(16000)这样的静态比特率值时,没有f的问题是一样的
-
你在
mixer.quit()之前试过mixer.music.stop()吗? -
是的,也没有用
-
从命令行尝试
chmod 777 googleTTS.mp3?
标签: python pygame google-text-to-speech