-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmedia_player_pyside6.py
148 lines (111 loc) · 4.63 KB
/
media_player_pyside6.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
import sys
from PySide6.QtWidgets import QMainWindow, QWidget, QVBoxLayout, QApplication, QSlider, QPushButton, QFileDialog, QHBoxLayout, QFrame
from PySide6.QtGui import QAction, QPalette, QColor
from PySide6.QtCore import Qt, QTimer
import vlc
class MediaPlayer(QMainWindow):
def __init__(self, master=None):
super().__init__(master)
self.setWindowTitle("Media Player")
# Create a basic vlc instance
self.instance = vlc.Instance()
self.media = None
# Create an empty vlc media player
self.mediaplayer = self.instance.media_player_new()
self.create_ui()
self.is_paused = False
def create_ui(self):
self.widget = QWidget(self)
self.setCentralWidget(self.widget)
self.videoframe = QFrame()
self.palette = self.videoframe.palette()
self.palette.setColor(QPalette.Window, QColor(0, 0, 0))
self.videoframe.setPalette(self.palette)
self.videoframe.setAutoFillBackground(True)
self.positionslider = QSlider(Qt.Orientation.Horizontal, self)
self.positionslider.setToolTip("Position")
self.positionslider.setMaximum(1000)
self.positionslider.sliderMoved.connect(self.set_position)
self.hbuttonbox = QHBoxLayout()
self.playbutton = QPushButton("Play")
self.hbuttonbox.addWidget(self.playbutton)
self.playbutton.clicked.connect(self.play_pause)
self.stopbutton = QPushButton("Stop")
self.hbuttonbox.addWidget(self.stopbutton)
self.stopbutton.clicked.connect(self.stop)
self.hbuttonbox.addStretch(1)
self.volumeslider = QSlider(Qt.Orientation.Horizontal, self)
self.volumeslider.setMaximum(100)
self.volumeslider.setValue(self.mediaplayer.audio_get_volume())
self.volumeslider.setToolTip("Volume")
self.hbuttonbox.addWidget(self.volumeslider)
self.volumeslider.valueChanged.connect(self.set_volume)
self.vboxlayout = QVBoxLayout()
self.vboxlayout.addWidget(self.videoframe)
self.vboxlayout.addWidget(self.positionslider)
self.vboxlayout.addLayout(self.hbuttonbox)
self.widget.setLayout(self.vboxlayout)
menu_bar = self.menuBar()
# File menu
file_menu = menu_bar.addMenu("File")
# Add actions to file menu
open_action = QAction("Load Video", self)
close_action = QAction("Close App", self)
file_menu.addAction(open_action)
file_menu.addAction(close_action)
open_action.triggered.connect(self.open_file)
close_action.triggered.connect(sys.exit)
self.timer = QTimer(self)
self.timer.setInterval(100)
self.timer.timeout.connect(self.update_ui)
self.volumeslider.setValue(50)
self.mediaplayer.audio_set_volume(50)
def play_pause(self):
if self.mediaplayer.is_playing():
self.mediaplayer.pause()
self.playbutton.setText("Play")
self.is_paused = True
self.timer.stop()
else:
if self.mediaplayer.play() == -1:
self.open_file()
return
self.mediaplayer.play()
self.playbutton.setText("Pause")
self.timer.start()
self.is_paused = False
def stop(self):
self.mediaplayer.stop()
self.playbutton.setText("Play")
def open_file(self):
dialog_txt = "Choose Media File"
filename, _ = QFileDialog.getOpenFileName(self, dialog_txt, os.path.expanduser('~'))
if not filename:
return
self.media = self.instance.media_new(filename)
self.mediaplayer.set_media(self.media)
self.media.parse()
self.setWindowTitle(self.media.get_meta(vlc.Meta.Title))
self.mediaplayer.set_hwnd(int(self.videoframe.winId()))
self.play_pause()
def set_volume(self, volume):
self.mediaplayer.audio_set_volume(volume)
def set_position(self, position):
pos = position / 1000.0
self.mediaplayer.set_position(pos)
def update_ui(self):
media_pos = int(self.mediaplayer.get_position() * 1000)
self.positionslider.setValue(media_pos)
if not self.mediaplayer.is_playing():
self.timer.stop()
if not self.is_paused:
self.stop()
def main():
app = QApplication(sys.argv)
player = MediaPlayer()
player.show()
player.resize(640, 480)
sys.exit(app.exec())
if __name__ == "__main__":
main()