Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gTTS (Google-Text-to-Speech) Error: Audio gets saved but does not play automatically

This is my code for text-to-speech conversion:

from gtts import gTTS
import os
tts=gTTS(text="Hello World",lang="en")
tts.save("hello.mp3")
os.system("mpg321 hello.mp3")

I already installed gTTS through the command prompt(pip install gTTS).

But when I'm running the above code in python 2.7, I am not getting any error but nothing is happening either.

I can't hear anything.

I am using Windows 10

Can you please advise what should be done?

like image 551
Blue Berry Avatar asked Sep 06 '25 03:09

Blue Berry


1 Answers

I had the same error. The problem was in the last line. Instead of os.system("mpg321 hello.mp3"), use the following:

os.system("start hello.mp3")

Since I save the text-to-speech converted audio in the working folder, it would suffice me to mention just the name of the audio inside the os.system.

Generally if you want to play an audio from the system, you would need to use this line:

os.system("start /thepathyouwant/filename")

The final working solution code:

from gtts import gTTS
import os
tts=gTTS(text="Hello World",lang="en")
tts.save("hello.mp3")
os.system("start hello.mp3")
like image 89
Steffi Keran Rani J Avatar answered Sep 07 '25 15:09

Steffi Keran Rani J