Omar
Omar

Reputation: 1

I tried to run a simple gTTS speech message but while the mp3 file is being made, i cant hear anything when i try to run it

I am using Sublime Text 3 and Python 3.7.6.

Here is the code I'm using;

from gtts import gTTS
tts = gTTS(text="Hello crazy programmer", lang='en')
tts.save("audio.mp3")

Upvotes: 0

Views: 411

Answers (1)

Will
Will

Reputation: 176

The above code just create a mp3 file, you have to use a mp3 player to play it. I would suggest using playsound library for simplicity but you can find more options here

from playsound import playsound
playsound("audio.mp3")

If this is not the case, you may check if GTTS save the mp3 file correctly by double-clicking in and play with your built-in mp3 player. For more safety, I suggest using something like this instead of your code:

        try:
            tts = gTTS(text=self.__speech, lang=self.__lang)
        except Exception as e:
            print("Failed to generate speech from text")
            removeSpeech()
            exit(e)
        else: #if no exception then save the file
            tts.save(self.__name_save)

Upvotes: 1

Related Questions