4

I'm trying to make a virtual assistant in python using the pyttsx3 modules. The problem is that my speak (audio) method that uses pyttsx3 only speaks once and even if I have to talk more than once, it remains silent after the first command.

The truth is that I have been looking on the internet and nobody seems to have a problem similar to the one I have and I am getting a little crazy. I have seen if it can be a problem that the module is not installed properly or something but it seems that everything is in order. I'm using Python 3.7 version, Pycharm IDE, Pyttsx3 last version and Windows10.

I attached the code to see if you know why it is. I have searched the internet but nobody seems to have that problem so I suppose it could be some error in my code. The output is Computer: Hello Sir, I am your digital assistant LARVIS the Lady Jarvis! ans should print the next line too:

import pyttsx3

engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[len(voices) - 2].id)


def speak(audio):
    print('Computer: ' + audio)
    engine.say(audio)
    engine.runAndWait()

if __name__ == '__main__':
    speak('Hello Sir, I am your digital assistant LARVIS the Lady Jarvis!')
    speak('How may I help you?')
UnaiLopez
  • 53
  • 1
  • 6

3 Answers3

5

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)
Léa Gris
  • 17,497
  • 4
  • 32
  • 41
Anon Helper
  • 51
  • 1
  • 2
1

I ran into this as well (also running python 3.7). It's not the best implementation, but mine works if I put the engine initialization in my speak function, so that it re-initializes each time it speaks.

Peter
  • 26
  • 5
1

I think you should install pyaudio module using pip install pyaudio. I faced a similar problem but it worked fine after installing pyaudio module.

Ritik Jain
  • 24
  • 2