I had this script working to hear an audio from text with gtts without having to save the mp3, using BytesIo, but now if I do not put a time.sleep at the end I cannot hear the sound. Why? Is there a solution different from putting a pause to the end?
from gtts import gTTS
from io import BytesIO
import pygame
import time
def speak(text, language='en'):
mp3_fo = BytesIO()
tts = gTTS(text, lang=language)
tts.write_to_fp(mp3_fo)
return mp3_fo
pygame.init()
pygame.mixer.init()
# sound.seek(0)
sound = speak("Python is cool always")
pygame.mixer.music.load(sound, 'mp3')
pygame.mixer.music.play()
time.sleep(5)