Blue Berry
Blue Berry

Reputation: 107

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?

Upvotes: 4

Views: 9289

Answers (1)

Steffi Keran Rani J
Steffi Keran Rani J

Reputation: 4093

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")

Upvotes: 2

Related Questions