Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python文本到语音转换未完成句子_Python_Class_Pyttsx - Fatal编程技术网

Python文本到语音转换未完成句子

Python文本到语音转换未完成句子,python,class,pyttsx,Python,Class,Pyttsx,所以,我试图用python3中的pyttsx创建一个对话引擎,当我第一次调用该函数时,它工作得很好,如果我再次调用它,它只会说出句子的第一个单词,什么也不会发生 import pyttsx class Speech(object): def __init__(self): self.engine = pyttsx.init() self.engine.setProperty('rate', 150) def say_song(self):

所以,我试图用python3中的pyttsx创建一个对话引擎,当我第一次调用该函数时,它工作得很好,如果我再次调用它,它只会说出句子的第一个单词,什么也不会发生

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的一个已知问题:

我会在Speech上写一个helper方法,基本上实现setup+say功能

def init_and_say(self, text):
    self.engine = pyttsx.init()
    self.engine.setProperty('rate', 150)
    self.engine.say(text)
    self.engine.runAndWait()
然后从每个方法调用它。e、 g:

def say_song(self):
    init_and_say("Please choose song. ")
或者直接称之为:

s.init_and_say("Please choose song. ")

它仍然有同样的问题。