How to play audio from text withou saving it with gtts and Python

preview_player
Показать описание
If you want to use gtts and python to save an mp3 audio created starting from a text is quite easy, but if you want to get the audio without saving anything, you need this code. It's quite simple, once you know how to do it, like for everything. Do not forget to subscribe.
Get the code here

try it online

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)
return mp3_fo

sound = speak("Python is cool always")

Alternatively you can do:
from gtts import gTTS

# Create an instance of the gTTS class
tts = gTTS('Hello, world!')

# Save the audio file as a temporary file

# The temporary audio file will be deleted when the script finishes executing

This is the last post about this topic, with new cool stuffs
Рекомендации по теме
Комментарии
Автор

Great video, thank you
btw i had a problem that imo it's worth to mention here. If i don't put time.sleep(5) to program it's not speak and directly shot it self.

ghostofdune
visit shbcf.ru