Search code examples
pythonclasspyttsx

Python text to speech not finishing the sentence


So, I'm trying to create a talking engine with pyttsx in python3, when I first call the function to say something it works fine, if I call it again, it just says the first word of the sentence and nothing happens.

import pyttsx


class Speech(object):

    def __init__(self):
        self.engine = pyttsx.init()
        self.engine.setProperty('rate', 150)

    def say_song(self):
        """ Tell user to choose song  """
        self.engine.say("Please choose song. ")
        self.engine.runAndWait()

    def say_alarm(self):
        """ Tell user to set up the alarm  """
        self.engine.say("Please set up the alarm, after the beep.")
        self.engine.runAndWait()

    def beep(self):
        self.engine.say("beep")
        self.engine.runAndWait()

>>> from voices import Speech
>>> s = Speech()
>>> s.say_song()
>>> s.beep()
>>> s.say_alarm()

Solution

  • It seems to be a known issue with pyttsx: https://github.com/RapidWareTech/pyttsx/issues/45

    I would write a helper method on Speech that basically does the setup + say functionality.

    def init_and_say(self, text):
        self.engine = pyttsx.init()
        self.engine.setProperty('rate', 150)
        self.engine.say(text)
        self.engine.runAndWait()
    

    then call it from each of your methods. e.g.:

    def say_song(self):
        init_and_say("Please choose song. ")
    

    or call it directly:

    s.init_and_say("Please choose song. ")