forked from PankajAS/SRSS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdplay.py
112 lines (89 loc) · 4.07 KB
/
mdplay.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
from PyQt5.QtGui import QIcon, QFont
from PyQt5.QtCore import QDir, Qt, QUrl, QSize
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import (QApplication, QFileDialog, QHBoxLayout, QLabel,
QPushButton, QSizePolicy, QSlider, QStyle, QVBoxLayout, QWidget, QStatusBar)
class VideoPlayer(QWidget):
def __init__(self, parent=None):
super(VideoPlayer, self).__init__(parent)
self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
btnSize = QSize(16, 16)
videoWidget = QVideoWidget()
openButton = QPushButton("Open Video")
openButton.setToolTip("Open Video File")
openButton.setStatusTip("Open Video File")
openButton.setFixedHeight(24)
openButton.setIconSize(btnSize)
openButton.setFont(QFont("Noto Sans", 8))
openButton.setIcon(QIcon.fromTheme("document-open", QIcon("D:/_Qt/img/open.png")))
openButton.clicked.connect(self.abrir)
self.playButton = QPushButton()
self.playButton.setEnabled(False)
self.playButton.setFixedHeight(24)
self.playButton.setIconSize(btnSize)
self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
self.playButton.clicked.connect(self.play)
self.positionSlider = QSlider(Qt.Horizontal)
self.positionSlider.setRange(0, 0)
self.positionSlider.sliderMoved.connect(self.setPosition)
self.statusBar = QStatusBar()
self.statusBar.setFont(QFont("Noto Sans", 7))
self.statusBar.setFixedHeight(14)
controlLayout = QHBoxLayout()
controlLayout.setContentsMargins(0, 0, 0, 0)
controlLayout.addWidget(openButton)
controlLayout.addWidget(self.playButton)
controlLayout.addWidget(self.positionSlider)
layout = QVBoxLayout()
layout.addWidget(videoWidget)
layout.addLayout(controlLayout)
layout.addWidget(self.statusBar)
self.setLayout(layout)
self.mediaPlayer.setVideoOutput(videoWidget)
self.mediaPlayer.stateChanged.connect(self.mediaStateChanged)
self.mediaPlayer.positionChanged.connect(self.positionChanged)
self.mediaPlayer.durationChanged.connect(self.durationChanged)
self.mediaPlayer.error.connect(self.handleError)
self.statusBar.showMessage("Ready")
def abrir(self):
fileName, _ = QFileDialog.getOpenFileName(self, "Selecciona los mediose",
".", "Video Files (*.mp4 *.flv *.ts *.mts *.avi)")
if fileName != '':
self.mediaPlayer.setMedia(
QMediaContent(QUrl("rtsp://admin:admin123@192.168.1.250")))
self.playButton.setEnabled(True)
self.statusBar.showMessage(fileName)
self.play()
def play(self):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
self.mediaPlayer.pause()
else:
self.mediaPlayer.play()
def mediaStateChanged(self, state):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
self.playButton.setIcon(
self.style().standardIcon(QStyle.SP_MediaPause))
else:
self.playButton.setIcon(
self.style().standardIcon(QStyle.SP_MediaPlay))
def positionChanged(self, position):
self.positionSlider.setValue(position)
print(position)
def durationChanged(self, duration):
self.positionSlider.setRange(0, duration)
def mediaStatusChanged(self, status):
self.statusBar.showMessage("Error: " + self.mediaPlayer.errorString())
def setPosition(self, position):
self.mediaPlayer.setPosition(position)
def handleError(self):
self.playButton.setEnabled(False)
self.statusBar.showMessage("Error: " + self.mediaPlayer.errorString())
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
player = VideoPlayer()
player.setWindowTitle("Player")
player.resize(600, 400)
player.show()
sys.exit(app.exec_())