Search code examples
pythonooppyttsx3

pyttsx3 say nothing


I am making a virtual assistant with python. I used pyttsx3 for talk to me. I wrote pyttsx3 initialisation in oops way. But when I run this code it says nothing. Here is my code.

import pyttsx3

class Speaker:
    
    def __init__(self):
        self.engine = pyttsx3.init() # Initializing pyttsx3
        self.voices = self.engine.getProperty('voices')  # Getting voices property
    
    def speaker_config(self):
        ''' Configurations for speaker '''   
        self.engine.getProperty('voices')  
        self.engine.setProperty('voice', self.voices[1].id)  # Changing voice male to female
        self.engine.setProperty('rate', 140)  # Changing the speed
    
    def say(self):
        self.engine.say('hello')
    
s1 = Speaker()
s1.say()```


Solution

  • You need to add engine.runAndWait() in order to process all the queued commands as well after doing an engine.say:

    def say(self):
        self.engine.say('hello')
        self.engine.runAndWait()
    

    See documentation.