关于PyQt5播放本地视频问题
程序员文章站
2022-04-26 16:03:21
...
import os
from Mainwindow import Ui_MainWindow
import sys
from PyQt5.QtMultimediaWidgets import QVideoWidget
# import RPi.GPIO as GPIO
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QMainWindow, QApplication, QVBoxLayout, QFileDialog
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
class Mainwin(Ui_MainWindow, QMainWindow):
def __init__(self):
super(Mainwin, self).__init__()
self.setupUi(self)
self.initUI()
def initUI(self):
self.player = QMediaPlayer()
self.player.setVideoOutput(self.widget)
self.cur_path = "" # 在此次写上要播放的文件路径
print("cur_path = ", self.cur_path)
self.player.setMedia(QMediaContent(QUrl(self.cur_path)))
self.player.play()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Mainwin()
window.show()
sys.exit(app.exec_())
下面是UI文件:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Mainwindow.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setWindowModality(QtCore.Qt.NonModal)
MainWindow.resize(800, 600)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Maximum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.widget = myVideoWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(50, 10, 681, 261))
self.widget.setObjectName("widget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(50, 319, 151, 101))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.widget_2 = QtWidgets.QWidget(self.verticalLayoutWidget)
self.widget_2.setObjectName("widget_2")
self.lineEdit = QtWidgets.QLineEdit(self.widget_2)
self.lineEdit.setGeometry(QtCore.QRect(20, 10, 111, 31))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.lineEdit.sizePolicy().hasHeightForWidth())
self.lineEdit.setSizePolicy(sizePolicy)
self.lineEdit.setAlignment(QtCore.Qt.AlignCenter)
self.lineEdit.setDragEnabled(False)
self.lineEdit.setReadOnly(True)
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout.addWidget(self.widget_2)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.widget_6 = QtWidgets.QWidget(self.verticalLayoutWidget)
self.widget_6.setObjectName("widget_6")
self.pushButton_9 = QtWidgets.QPushButton(self.widget_6)
self.pushButton_9.setGeometry(QtCore.QRect(0, 0, 71, 51))
self.pushButton_9.setText("")
self.pushButton_9.setObjectName("pushButton_9")
self.horizontalLayout.addWidget(self.widget_6)
self.widget_7 = QtWidgets.QWidget(self.verticalLayoutWidget)
self.widget_7.setObjectName("widget_7")
self.pushButton_2 = QtWidgets.QPushButton(self.widget_7)
self.pushButton_2.setGeometry(QtCore.QRect(0, 0, 71, 51))
self.pushButton_2.setText("")
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.widget_7)
self.verticalLayout.addLayout(self.horizontalLayout)
self.verticalLayoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(220, 320, 151, 101))
self.verticalLayoutWidget_2.setObjectName("verticalLayoutWidget_2")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_2)
self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.widget_3 = QtWidgets.QWidget(self.verticalLayoutWidget_2)
self.widget_3.setObjectName("widget_3")
self.lineEdit_2 = QtWidgets.QLineEdit(self.widget_3)
self.lineEdit_2.setGeometry(QtCore.QRect(20, 10, 111, 31))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.lineEdit_2.sizePolicy().hasHeightForWidth())
self.lineEdit_2.setSizePolicy(sizePolicy)
self.lineEdit_2.setAlignment(QtCore.Qt.AlignCenter)
self.lineEdit_2.setReadOnly(True)
self.lineEdit_2.setObjectName("lineEdit_2")
self.verticalLayout_2.addWidget(self.widget_3)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.widget_8 = QtWidgets.QWidget(self.verticalLayoutWidget_2)
self.widget_8.setObjectName("widget_8")
self.pushButton_3 = QtWidgets.QPushButton(self.widget_8)
self.pushButton_3.setGeometry(QtCore.QRect(0, 0, 71, 51))
self.pushButton_3.setText("")
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout_2.addWidget(self.widget_8)
self.widget_9 = QtWidgets.QWidget(self.verticalLayoutWidget_2)
self.widget_9.setObjectName("widget_9")
self.pushButton_4 = QtWidgets.QPushButton(self.widget_9)
self.pushButton_4.setGeometry(QtCore.QRect(0, 0, 71, 51))
self.pushButton_4.setText("")
self.pushButton_4.setObjectName("pushButton_4")
self.horizontalLayout_2.addWidget(self.widget_9)
self.verticalLayout_2.addLayout(self.horizontalLayout_2)
self.verticalLayoutWidget_3 = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget_3.setGeometry(QtCore.QRect(400, 320, 151, 101))
self.verticalLayoutWidget_3.setObjectName("verticalLayoutWidget_3")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_3)
self.verticalLayout_3.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.widget_4 = QtWidgets.QWidget(self.verticalLayoutWidget_3)
self.widget_4.setObjectName("widget_4")
self.lineEdit_3 = QtWidgets.QLineEdit(self.widget_4)
self.lineEdit_3.setGeometry(QtCore.QRect(20, 10, 111, 31))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.lineEdit_3.sizePolicy().hasHeightForWidth())
self.lineEdit_3.setSizePolicy(sizePolicy)
self.lineEdit_3.setAlignment(QtCore.Qt.AlignCenter)
self.lineEdit_3.setReadOnly(True)
self.lineEdit_3.setObjectName("lineEdit_3")
self.verticalLayout_3.addWidget(self.widget_4)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.widget_10 = QtWidgets.QWidget(self.verticalLayoutWidget_3)
self.widget_10.setObjectName("widget_10")
self.pushButton_5 = QtWidgets.QPushButton(self.widget_10)
self.pushButton_5.setGeometry(QtCore.QRect(0, 0, 71, 51))
self.pushButton_5.setText("")
self.pushButton_5.setObjectName("pushButton_5")
self.horizontalLayout_3.addWidget(self.widget_10)
self.widget_11 = QtWidgets.QWidget(self.verticalLayoutWidget_3)
self.widget_11.setObjectName("widget_11")
self.pushButton_6 = QtWidgets.QPushButton(self.widget_11)
self.pushButton_6.setGeometry(QtCore.QRect(0, 0, 71, 51))
self.pushButton_6.setText("")
self.pushButton_6.setObjectName("pushButton_6")
self.horizontalLayout_3.addWidget(self.widget_11)
self.verticalLayout_3.addLayout(self.horizontalLayout_3)
self.verticalLayoutWidget_4 = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget_4.setGeometry(QtCore.QRect(580, 320, 151, 101))
self.verticalLayoutWidget_4.setObjectName("verticalLayoutWidget_4")
self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_4)
self.verticalLayout_4.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.widget_5 = QtWidgets.QWidget(self.verticalLayoutWidget_4)
self.widget_5.setObjectName("widget_5")
self.lineEdit_4 = QtWidgets.QLineEdit(self.widget_5)
self.lineEdit_4.setGeometry(QtCore.QRect(20, 10, 111, 31))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.lineEdit_4.sizePolicy().hasHeightForWidth())
self.lineEdit_4.setSizePolicy(sizePolicy)
self.lineEdit_4.setAlignment(QtCore.Qt.AlignCenter)
self.lineEdit_4.setReadOnly(True)
self.lineEdit_4.setObjectName("lineEdit_4")
self.verticalLayout_4.addWidget(self.widget_5)
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.widget_12 = QtWidgets.QWidget(self.verticalLayoutWidget_4)
self.widget_12.setObjectName("widget_12")
self.pushButton_7 = QtWidgets.QPushButton(self.widget_12)
self.pushButton_7.setGeometry(QtCore.QRect(0, 0, 71, 51))
self.pushButton_7.setText("")
self.pushButton_7.setObjectName("pushButton_7")
self.horizontalLayout_4.addWidget(self.widget_12)
self.widget_13 = QtWidgets.QWidget(self.verticalLayoutWidget_4)
self.widget_13.setObjectName("widget_13")
self.pushButton_8 = QtWidgets.QPushButton(self.widget_13)
self.pushButton_8.setGeometry(QtCore.QRect(0, 0, 71, 51))
self.pushButton_8.setText("")
self.pushButton_8.setObjectName("pushButton_8")
self.horizontalLayout_4.addWidget(self.widget_13)
self.verticalLayout_4.addLayout(self.horizontalLayout_4)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.lineEdit.setText(_translate("MainWindow", "可回收垃圾"))
self.lineEdit_2.setText(_translate("MainWindow", "厨余垃圾"))
self.lineEdit_3.setText(_translate("MainWindow", "不可回收垃圾"))
self.lineEdit_4.setText(_translate("MainWindow", "其他垃圾"))
from myvideowidget import myVideoWidget
接下来是myvideowidget的代码:
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtCore import *
class myVideoWidget(QVideoWidget):
def __init__(self, parent=None):
super(QVideoWidget, self).__init__(parent)
在写要播放的文件路径的时候一定要用///,file:///。注意一定是三个斜杠。