I am running the following script, there is no error but there is no sound. I am not sure how I can make the text really speak through the speakers. Any idea ?
from gtts import gTTS
#Write your text
text_to_speech = "Text to speech using Python"
#Choose your language (en : english, fr: french, ...)
language = "en"
i = 1
while i <= 5:
#Passing the text, language and the speed to the module
myobj = gTTS(text=text_to_speech, lang=language, slow=False)
print(i)
i=i+1
You have to use myobj.save to save to an mp3 file, and then play the
mp3 file using whatever program you would normally use to play mp3
files. On Linux, you may be able to use mpg123.
Also, you should not use a while loop.
If you are running Linux, and you have mpg123 installed, you can try
this:
import os
from gtts import gTTS
text_to_speech = "Text to speech using Python"
myobj = gTTS(text=text_to_speech, lang='en', slow=False)
myobj.save('my_text.mp3')
for i in range(5):
print(i)
os.system('mpg123 my_text.mp3')
I am using Windows. If I add the following code then it save the mp3 file which I can play, that’s fine but I wat to make the text speak in real time and not saving in mp3 format.
#Saving the audio in mp3
myobj.save("text_to_speech.mp3")