1

我使用 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 函数中,因为没有混音器的代码可以正常工作。

如何释放混音器资源并一遍又一遍地使用同一个文件?

4

1 回答 1

0

这种方式对我有用:

pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
    clock.tick(30)
pygame.mixer.music.stop()
pygame.mixer.quit()
于 2017-11-12T01:15:21.853 回答