omxplayer と tkinter を使用して、Raspberry Pi Media Player を作成しようとしています。
USB ドライブ (またはローカル フォルダー) から最大 16 個のビデオを取得し、ボタンのグリッドにサムネイルとして表示します。ボタンを押すと、そのビデオが omxplayer でフルスクリーンで再生されます (その時点で、ビデオが終了するまで tkinter ウィンドウにアクセスできなくなります)。基本的に、ビデオがまだ再生されていない場合にのみ、ビデオの選択を有効にしたいと考えています。
私が直面している問題は、1 つのボタンを複数回押すか、ビデオが再生される前に他のボタンを押すことです。これにより、すべてのビデオがキューに入れられ、すぐに次から次へと再生されます。最初のビデオ(最初のボタンを押してから再生)の再生が完了するまで、後続のプレスを登録したくありません。ブール変数 video_is_playing を設定し、関数呼び出しでその状態を確認しようとしましたが、else ケースにはなりません。
いずれかのボタンを押した後にすべてのボタンを無効にしてから、ビデオの再生が終了した後にすべてのボタンを有効にしようとしました。変数を使用して、ビデオの再生が終了したかどうかを確認しようとしました。複数のボタンを押すのを防ぎます。
これが私のコードの一部です(長く見える場合は申し訳ありませんが、含まれているすべてが関連していると思います):
class TkinterGUI:
def __init__(self):
self.folder_name="videos"
self.vid_path = f"{os.path.split(os.getcwd())[0]}/{os.path.split(os.getcwd())[1]}/{self.folder_name}/"
self.videos = []
self.video_is_playing = False
self.vidbuttons = []
for f in os.listdir(f"{self.vid_path}"):
if '.mp4' in f:
self.videos.append(f)
self.videos.sort()
self.videos_iterator = iter(self.videos)
def pack_thumbnail(self, path, button):
#putting video thumbnail in button with imageio
pass
def disable_buttons(self, window):
for b in self.vidbuttons:
b.config(state=tk.DISABLED)
window.update()
print(">>all buttons diabled")
def enable_buttons(self, window):
for b in self.vidbuttons:
b.config(state=tk.NORMAL)
window.update()
print(">>all buttons enabled")
def play_vid(self, i, j, window):
try:
self.disable_buttons(window)
if self.video_is_playing == False:
self.video_is_playing=True
k = (i*4)+j
video = self.videos[k]
path = f"{self.vid_path}/{video}"
print(f">>now playing: {video} of duration {self.vid_duration(path)}")
omxp = Popen(['omxplayer', path])
omxp.wait()
print(f"video {video} is done playing!")
else:
print("a video seems to be playing already")
return
except Exception as e:
print(e)
finally:
self.video_is_playing = False
self.enable_buttons(window)
def video_player_window(self):
window = tk.Tk()
window.attributes("-fullscreen", True)
#left side frame(blank for now)
frame1 = tk.Frame(master=window, width=200, height=100, bg="white")
frame1.pack(fill=tk.Y, side=tk.LEFT)
#main video player frame(contains 4x4 grid of video thumbnails)
frame2 = tk.Frame()
for i in range(4):
frame2.columnconfigure(i, weight=1, minsize=75)
frame2.rowconfigure(i, weight=1, minsize=50)
for j in range(4):
frame = tk.Frame(master=frame2, relief=tk.FLAT, borderwidth=1)
frame.grid(row=i, column=j, padx=5, pady=5)
vid=next(self.videos_iterator, "end")
print(vid)
if vid != "end":
button = tk.Button(master=frame, highlightcolor="black", text=f"Row {i}\nColumn {j}", command= partial(self.play_vid, i, j, window))
self.pack_thumbnail(self.vid_path+f"{vid}", button)
button.pack(padx=5, pady=5)
self.vidbuttons.append(button)
else:
img = Image.open(f"vidnotfound.png")
img = img.resize((424, 224))
image = ImageTk.PhotoImage(img)
label = tk.Label(master=frame, text=f"Row {i}\nColumn {j}", image=image)#, compound='center')
label.image = image
label.pack(padx=5, pady=5)
frame2.pack()
window.bind("<Escape>", lambda x: window.destroy())
window.mainloop()
tkin = TkinterGUI()
tkin.video_player_window()
functools.partial() を使用して、i、j インデックスを play_vid 関数に渡しました。これにより、これらのインデックスを使用して、リストから再生するビデオを知ることができます。ここに私がインポートしたすべてがあります:
import tkinter as tk
import imageio
from PIL import ImageTk, Image
from pathlib import Path
from functools import partial
import subprocess
from subprocess import Popen
余談ですが、ボタングリッドでやりたいことを達成するためのより良い方法はありますか? 各ボタンで同じ関数を呼び出して別のビデオを再生したいのですが、使用できる属性などはありますか?