from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import *
from PyQt5 import QtCore, QtGui, QtWidgets
from UI.UImain import *
class VideoWidget(QVideoWidget):
def __init__(self, parent=None):
super(VideoWidget, self).__init__(parent)
self.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
p = self.palette()
p.setColor(QPalette.Window, Qt.black)
self.setPalette(p)
self.setAttribute(Qt.WA_OpaquePaintEvent)
def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape and self.isFullScreen():
self.setFullScreen(False)
event.accept()
elif event.key() == Qt.Key_Enter and event.modifiers() & Qt.Key_Alt:
self.setFullScreen(not self.isFullScreen())
event.accept()
else:
super(VideoWidget, self).keyPressEvent(event)
def mouseDoubleClickEvent(self, event):
self.setFullScreen(not self.isFullScreen())
event.accept()
class ui_C(QWidget):
def __init__(self, parent=None):
super(ui_C, self).__init__(parent)
self.player = QMediaPlayer()
self.playlist = QMediaPlaylist()
self.player.setPlaylist(self.playlist)
self.videoWidget = VideoWidget()
fileInfo = QFileInfo('UIpic/video1.mp4')
url = QUrl.fromLocalFile(fileInfo.absoluteFilePath())
self.playlist.addMedia(QMediaContent(url))
def play_video(self):
print('video1 playing~!!!!')
self.videoWidget.setFullScreen(True)
self.player.setVideoOutput(self.videoWidget)
# 当前播放的曲目
self.playlist.setCurrentIndex(0)
print('现在播放的视频“',self.playlist.currentIndex)
self.playlist.setPlaybackMode(QMediaPlaylist.CurrentItemInLoop)
self.player.play()
def statusChanged(self, status):
if status == QMediaPlayer.EndOfMedia:
self.videoWidget.close()
def keyPressEvent(self, e):
if e.key() == Qt.Key_L:
print('press L')
if __name__ == "__main__":
app = QApplication(sys.argv)
c = ui_C()
c.play_video()
sys.exit(app.exec_())