【问题标题】:How to free resource in PyGame mixer?如何在 PyGame 混音器中释放资源?
【发布时间】: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 什么的吗?不知道MP3 class(?) 做了什么。
  • 不,当我使用像mixer.init(16000)这样的静态比特率值时,没有f的问题是一样的
  • 你在mixer.quit()之前试过mixer.music.stop()吗?
  • 是的,也没有用
  • 从命令行尝试chmod 777 googleTTS.mp3

标签: python pygame google-text-to-speech


【解决方案1】:

这种方式适合我:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 2019-06-23
    • 2021-03-03
    相关资源
    最近更新 更多