I'm trying to make a simple Python script which should take a text as input and using gtts, play it as audio. I'm using the following code, but it's not working and I don't know why.
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 version 3.9.1
gTTS version 2.2.1
pygame version 2.0.1
Windows 10
EDIT: This is the complete error I get:
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