我是python的新手,我在使用离线tts按下Raspberry Pi 3上的按钮后试图产生声音,我的按钮连接到引脚16(BCM)。
我有一个无限运行的循环,只在单击按钮后尝试发出一个单词。
运行脚本并按下按钮后,它会记录一次
b4 roboticvoice
这很好,因为即使我按下了按下的按钮,该部分只运行一次,但事实是它有时会在该尝试时冻结,有时它会在冻结前运行3,4或5次。 我注意到它是冻结时的堆栈,每当我重新开始时,脚本会说明函数中的前一个单词(暗示你已经改变了它)。
我已尝试使用pyttsx3,espeak both,我知道pyttsx3包含espeak。 我也试过线程无济于事。 这里简化了我的代码:
# coding=utf-8
from gtts import gTTS
import os
import pyttsx3
import RPi.GPIO as GPIO
import vlc
from gpiozero import Button
import time
import threading
GPIO.setmode(GPIO.BCM)
GPIO.setup(16, GPIO.IN)
language_pref = "es"
clickCount = 0
button = Button(23,False)
buttonMaxDelay = 1
tiempo = time.time()
threads = list()
engine = pyttsx3.init()
rate = engine.getProperty('rate')
engine.setProperty('rate', rate)
voices= engine.getProperty('voices')
engine.setProperty('voice', 'spanish')
#engine.setProperty('voice', 'english')
def roboticVoice2(texto):
engine.say(texto)
a = engine.runAndWait()
def roboticVoice(texto):
os.system('espeak -ves+m7 "{0}" 2>/dev/null'.format(texto))
def increaseClickCount():
global clickCount
global tiempo
clickCount+=1
tiempo = time.time()
button.when_pressed = increaseClickCount
while True:
if (time.time() > (tiempo + buttonMaxDelay)) and clickCount > 0:
#this part is for multiple clicks, to be used later
print("b4 roboticvoice")
clickCount = 0
#roboticVoice2("say something")
rb = threading.Thread(target=roboticVoice2, args=["Hola"])
threads.append(rb)
rb.start()
rb.join()
任何建议都会被贬低。