|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
import tkinter as tk
from tkinter import filedialog
import vlc
import os
import platform
class VLCPlayer:
def __init__(self, root):
self.root = root
self.root.title("Tkinter VLC 影片播放器")
self.root.geometry("800x600")
# 建立 VLC 播放器實例
self.instance = vlc.Instance()
self.player = self.instance.media_player_new()
# 影片播放區域 Frame
self.video_panel = tk.Frame(self.root)
self.video_panel.pack(fill="both", expand=1)
# 控制按鈕區
self.btn_frame = tk.Frame(self.root)
self.btn_frame.pack(pady=10)
self.open_btn = tk.Button(self.btn_frame, text="開啟影片", command=self.open_file)
self.open_btn.grid(row=0, column=0, padx=5)
self.play_btn = tk.Button(self.btn_frame, text="播放", command=self.play)
self.play_btn.grid(row=0, column=1, padx=5)
self.pause_btn = tk.Button(self.btn_frame, text="暫停", command=self.pause)
self.pause_btn.grid(row=0, column=2, padx=5)
self.stop_btn = tk.Button(self.btn_frame, text="停止", command=self.stop)
self.stop_btn.grid(row=0, column=3, padx=5)
self.media = None
self.root.update() # 確保元件建立完成
# 取得影片播放視窗 handle,依系統設置
self.handle = self.video_panel.winfo_id()
sys_platform = platform.system()
if sys_platform == "Windows":
self.player.set_hwnd(self.handle)
elif sys_platform == "Linux":
self.player.set_xwindow(self.handle)
elif sys_platform == "Darwin": # macOS
self.player.set_nsobject(self.handle)
def open_file(self):
filepath = filedialog.askopenfilename(filetypes=[("影片檔案", "*.mp4;*.avi;*.mov;*.mkv")])
if filepath:
self.media = self.instance.media_new(filepath)
self.player.set_media(self.media)
self.play()
def play(self):
if self.media:
self.player.play()
def pause(self):
self.player.pause()
def stop(self):
self.player.stop()
if __name__ == "__main__":
# 設定 VLC 的 DLL 路徑(Windows 必要)
os.environ['PATH'] = r'C:\Program Files\VideoLAN\VLC' + ';' + os.environ['PATH']
root = tk.Tk()
app = VLCPlayer(root)
root.mainloop()
|