简体   繁体   English

Pyttsx3 只会说一次

[英]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如果我主要注释掉 Speakit,脚本运行正确,说出打印出来的内容

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如果我没有在主体中注释掉 Speakit,脚本只会说出“开始”这个词,然后不再说话,只是挂起

 python3 mp2.py
running Speakit
0
running Speakit

Don't understand why不明白为什么

I have given your code a quick spin.我已经快速旋转了您的代码。 At first, the interpreter threw some major errors due to the initialisation of your pyttsx engine.起初,由于您的 pyttsx 引擎的初始化,解释器抛出了一些重大错误。 I deleted the 'espeak' argument and then it did work and the program also did speak both lines.我删除了'espeak'参数,然后它确实起作用了,该程序也确实说出了两条线。

Maybe use try/except in order to troubleshoot.也许使用try/except来排除故障。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM