我正在尝试制作一个简单的 Python 脚本,它应该将文本作为输入并使用 gtts,将其作为音频播放。我正在使用以下代码,但它不起作用,我不知道为什么。
from gtts import gTTS
import pygame
from io import BytesIO
pygame.init()
def say(text):
tts = gTTS(text=text, lang='en')
fp = BytesIO()
tts.write_to_fp(fp)
fp.seek(0)
pygame.mixer.init()
pygame.mixer.music.load(fp)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
say("hello")
Python 版本 3.9.1
gTTS 版本 2.2.1
pygame 版本 2.0.1 Windows 10
编辑:这是我得到的完整错误:
pygame 2.0.1 (SDL 2.0.14, Python 3.9.1)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "c:\Users\Marco\Documents\Programmazione\gtts\main2.py", line 19, in <module>
say("hello")
File "c:\Users\Marco\Documents\Programmazione\gtts\main2.py", line 14, in say
pygame.mixer.music.load(fp)
pygame.error: ModPlug_Load failed