首页 > 解决方案 > Pyttsx3 Will only Speak Once

问题描述

When running the following program:

import multiprocessing
import pyttsx3
from multiprocessing import Process


class _TTS:

   def __init__(self):
        self.engine = pyttsx3.init('espeak')
        self.engine.setProperty('rate', 175)

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

def Speakit(words):
    print('running Speakit')
    
    tts = _TTS()
    tts.start(words)
    del(tts)

def testing(n):
    print(n)
    if n == 0:
        words = 'Argument is zero'
        Speakit(words)
        print(words)
    else:
        words = 'Argument is not zero'
        Speakit(words)
        print(words)

if __name__=="__main__":
   words = 'start'
 #  Speakit(words)
   p1=Process(target=testing,args=(0,))
   p1.start()
   p1.join()
   p2=Process(target=testing,args=(5,))
   p2.start()
   p2.join()
   print("We're done")

If I comment out the Speakit in the main, the script run correctly, speaking what prints out

Watson $ python3 mp2.py
0
running Speakit
Argument is zero
5
running Speakit
Argument is not zero
We're done

It I don't comment out the Speakit in the main, the script will just speak the "Start" word and then not speak again and just hangs

 python3 mp2.py
running Speakit
0
running Speakit

Don't understand why

标签: pythonmultiprocessingpyttsx

解决方案


我已经快速旋转了您的代码。起初,由于您的 pyttsx 引擎的初始化,解释器抛出了一些重大错误。我删除了这个'espeak'论点,然后它确实起作用了,程序也确实说出了这两行。

也许用于try/except排除故障。


推荐阅读