3

I have recently look into using Pyttsx in Python 2.7 for a project I'm doing, but after it has finished speaking the program hangs and won't continue.

Here is my code:

import pyttsx

engine = pyttsx.init()

engine.say("Hello world.")
engine.runAndWait()

After running engine.runAndWait(), it doesn't run anything else and hangs without returning anything. I tried adding a print afterwards, but it didn't print anything.

Is this common for anyone else, and is there a fix for this? If not then is there a good alternative? (Aside from Google TTS)

Thanks!

---------EXTRA-------- I use a Macbook Pro with OS X El Capitan 10.11.4

Nikolay Shmyrev
  • 24,897
  • 5
  • 43
  • 87
TechnoSwag
  • 385
  • 1
  • 4
  • 11

3 Answers3

1

This is a bug in pyttsx, you have to wait until it is resolved

https://github.com/RapidWareTech/pyttsx/issues/26

Nikolay Shmyrev
  • 24,897
  • 5
  • 43
  • 87
1

To deal with this problem, 1- make a class for pyttsx3; 2- make an instance of the class, send the text to it, then del() it. 3- repeat step 2 several times.

the Class:

import pyttsx3

class _TTS:

    engine = None
    rate = None
    def __init__(self):
        self.engine = pyttsx3.init()


    def start(self,text_):
        self.engine.say(text_)
        self.engine.runAndWait()

the instance:

 tts = _TTS()
 tts.start("text")
 del(tts)
Arash
  • 51
  • 3
  • Wow! This actually works. (Running pyttsx3 on MacOS took minutes for `runAndWait` to finish, compared to 1 second per sentence this way.) – Marc Maxmeister Apr 25 '22 at 20:25
0

This issue has been resolved in pyttsx3.

!pip install pyttsx3

import pyttsx3 as p
text = "The issue has been resolved"
p.speak(text)
Samit Saxena
  • 99
  • 1
  • 9