PyQt5视频播放器**
用到了QMediaPlayer
此播放器主要功能是播放本地视频音频,具有调节音量作用,搜索列表内容作用以及看全屏能力
实现了暂停与播放在同一按键
1.自定义的widget。myvideowidget.py**
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtCore import *
class myVideoWidget(QVideoWidget):
doubleClickedItem = pyqtSignal(str) # 创建双击信号
def __init__(self,parent=None):
super(QVideoWidget,self).__init__(parent)
def mouseDoubleClickEvent(self,QMouseEvent): #双击事件
self.doubleClickedItem.emit("double clicked")
2.将做的QtDesiger MainWindow.ui编译生成Ui_MainWindow.py并将上层自定义的widge导入到Ui_MainWindow.py中
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'd:\python project\视频播放器\MainWindow.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(741, 542)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(-1, -1, 661, 461))
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.horizontalSlider = QtWidgets.QSlider(self.centralwidget)
self.horizontalSlider.setGeometry(QtCore.QRect(40, 470, 561, 22))
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName("horizontalSlider")
self.verticalSlider = QtWidgets.QSlider(self.centralwidget)
self.verticalSlider.setGeometry(QtCore.QRect(690, 20, 22, 331))
self.verticalSlider.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider.setObjectName("verticalSlider")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(660, 400, 81, 21))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(150, 510, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(370, 510, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
MainWindow.setCentralWidget(self.centralwidget)
#定义框架
self.widget = myVideoWidget(self.frame)
self.widget.setObjectName("widget")
self.widget.setGeometry(5,5,640,480)
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(MainWindow.choose)
self.pushButton_2.clicked.connect(MainWindow.play)
self.horizontalSlider.sliderPressed.connect(MainWindow.go)
self.verticalSlider.sliderPressed.connect(MainWindow.voice)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "音量:"))
self.pushButton.setText(_translate("MainWindow", "选择视频"))
self.pushButton_2.setText(_translate("MainWindow", "暂停/播放"))
#将构建的框架导入
from myvideowidget import myVideoWidget
3.源代码
# 系统包
import sys
import os
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QApplication,QMainWindow,QFileDialog
from PyQt5.QtMultimedia import *
# 项目包
from Ui_MainWindow import Ui_MainWindow
class CMainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent = None):
super(CMainWindow, self).__init__(parent)
self.setupUi(self)
# 主窗体设置
self.show()
self.player = QMediaPlayer()
self.player.positionChanged.connect(self.go)
self.player.setVideoOutput(self.widget)
让播放器可以从文件夹中找到视频和音频文件并将其放置于播放器框架中
def choose(self):
self.cur_path = os.path.abspath(os.path.dirname(__file__))
self.mp4list = []
self.shipin_formats = ['mp4']
self.cur_path = QFileDialog.getExistingDirectory(self, "选取文件夹", self.cur_path)
self.qlist.clear()
for shipin in os.listdir(self.cur_path):
if shipin.split('.')[-1] in self.shipin_formats:
self.mp4list.append([shipin, os.path.join(self.cur_path, shipin).replace('\\', '/')])
self.qlist.addItem(shipin)
self.qlist.setCurrentRow(0)
if self.mp4list:
self.play_mp4 = self.mp4list[self.qlist.currentRow()][-1]
self.play_mp4 = self.mp4list[self.qlist.currentRow()][-1]
self.player.setMedia(QMediaContent(QUrl(self.play_mp4)))
self.player.play()
暂停和开始按钮
def play(self):
if self.player.state()==1:
self.player.pause()
else:
self.player.play()
读取进度条
def go(self,position):
self.vidoeLength = self.player.duration() + 0.1
self.horizontalSlider.setValue(round((position / self.vidoeLength) * 100))
音量调节按钮
def voice(self):
self.verticalSlider.setMaximum(100)
self.verticalSlider.setMinimum(0)
s = self.verticalSlider.value()
print(s)
self.player.setVolume(s)
双击屏幕可以使视频全屏播放
再次双击使得缩小
def doubleClicked(self):
self.setCurPlaying()
self.player.play()
效果展示