0

所以,我正在尝试pyttsx在 python3 中创建一个谈话引擎,当我第一次调用该函数说它工作正常时,如果我再次调用它,它只会说出句子的第一个单词而没有任何反应。

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()
4

1 回答 1

0

这似乎是 pyttsx 的一个已知问题: https ://github.com/RapidWareTech/pyttsx/issues/45

我会在 Speech 上编写一个辅助方法,该方法基本上完成了设置 + 说功能。

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

然后从您的每个方法中调用它。例如:

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

或直接调用它:

s.init_and_say("Please choose song. ")
于 2017-05-07T20:42:08.793 回答