0

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")
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • 1
    What do you mean by "run it" here? The code looks like it's creating an `mp3` file, so I would expect that the result of running this would be that an `mp3` would fall out of the program. Are you trying to play the resulting MP3 and it's silent? – OdatNurd Dec 30 '19 at 08:09
  • Why is this tagged as a Sublime Text question? Because it's on your machine? – the Tin Man Dec 31 '19 at 23:11

1 Answers1

1

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)

Will
  • 166
  • 1
  • 9
  • Would not recommend using exit inside the exception block, just `raise` the error if you don't want to ignore it. – moshevi Dec 31 '19 at 23:16