i making a program that plays a video when it matches with a word that a person says, if it matches, the video shows in a tkinter label.My problem is when it matches more than one word, because the program plays all the videos that matches at once.I want to play all the videos that matches with the words one after another.Im using tkinter and tkvideo . Hope you can help me with this one.
dir = 'C:\\Users\\Usuario\\Desktop\\Prototipo\\palabras'
listaVid = []
with os.scandir(dir) as ficheros:
for fichero in ficheros:
listaVid.append(fichero.name.replace('.mp4',''))
def audioTexto():
listener = sr.Recognizer()
with sr.Microphone(device_index=0) as source:
print("Escuchando...")
audio = listener.listen(source)
rec = listener.recognize_whisper(audio, language="spanish")
trans_tab = dict.fromkeys(map(ord, u'\u0301\u0308'), None)
rec = normalize('NFKC', normalize('NFKD', rec).translate(trans_tab))
rec = re.sub(r'[.]', '', rec)
palabras = rec.split(',')
try:
for palabra in palabras:
for vid in listaVid:
if vid in palabra:
player = tkvideo.tkvideo('C:\\Users\\Usuario\\Desktop\\Reconocimiento de voz\\palabras\\'+vid+'.mp4', lblVid,loop=0, size = (640,360))
player.play()
except sr.UnknownValueError:
print("No se entiende")
except sr.RequestError as e:
print("error")
root =Tk()
btnHablar = Button(root,text='Hablar..',command= audioTexto)
btnHablar.grid(column=0, row=0 , padx=5 ,pady=5)
lblVid = Label(root)
lblVid.grid(column=0,row=2,columnspan=2)
root.mainloop()
You need to store the required videos in a list and go through the list one by one.
In order to play the videos one by one, you can overload the class method load()
of tkvideo
to signal the application using virtual event when the current playing video ends. When the application receives the virtual event, it can start the next video.
Below is an example:
import tkinter as tk
import tkvideo
# create custom class based on tkvideo
class Player(tkvideo.tkvideo):
# overload load()
def load(self, path, label, loop):
# call the original load()
super().load(path, label, loop)
# video ended, signal the application via virtual event
label.event_generate("<<End>>")
# function to play a video in the playlist
def play():
try:
# get next video from the playlist
video = next(playlist)
print(f"{video=}")
# play the video
player = Player(video, playbox)
player.play()
except Exception as ex:
# no more video to play
print(f"{ex=}")
# create the playlist
playlist = iter(["1.mp4", "2.mp4", "3.mp4"])
root = tk.Tk()
playbox = tk.Label(root)
playbox.pack()
# call play() upon receiving virtual event "<<End>>"
playbox.bind("<<End>>", lambda e: play())
# play the first video
play()
tk.mainloop()