Python 文字转语音未完成句子

Python text to speech not finishing the sentence

所以,我正在尝试用 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()

这似乎是 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. ")