pythonpygamegoogle-text-to-speechgttspygame-mixer

GttS Wait Until words is complete Python


I am using gtts to read words from a list and say them one by one. The current problem I am facing is that when I play the words one by one in the list they start at the same time. Here is the reproduced result:

def speak(text,language="en",accent="com"):
    
    mp3_fp = BytesIO()
    phrase = gTTS(text=text,lang=language,tld=accent)
    phrase.write_to_fp(mp3_fp)
    pygame.init()
    pygame.mixer.init()
    pygame.mixer.music.load(mp3_fp,"mp3")
    pygame.mixer.music.play()

def play():
        data = list[boy, girl, small, big]
        for i in data:
            speak(i)

You can see that I try to play the words directly from gtts instead of having to save them. If I saved them individually would there be any benefit that might help me?


Solution

  • You must wait until the word has been spoken respectively the sound has ended. e.g.:

    pygame.mixer.music.play()
    while pygame.mixer.music.get_busy():
        pygame.time.delay(10)
        pygame.event.poll()