如何在 QtVideoWigdet 中加载路径
How to load a path in QtVideoWigdet
我有一个关于pyqt5的问题。我想制作一个视频播放器。现在我被困住了,我怎么能给他们完整的路径而不是点击按钮并转到路径和 select 视频我只想要它这个文件路径。请看看我的代码。我为 raspberry pi 和 window
做了这个
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_VideoWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(733, 505)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.Video_Widget = QVideoWidget(self.centralwidget)
self.Video_Widget.setGeometry(QtCore.QRect(0, 0, 711, 361))
self.Video_Widget.setObjectName("Video_Widget")
self.horizontalScrollBar = QtWidgets.QScrollBar(self.centralwidget)
self.horizontalScrollBar.setGeometry(QtCore.QRect(0, 380, 721, 16))
self.horizontalScrollBar.setOrientation(QtCore.Qt.Horizontal)
self.horizontalScrollBar.setObjectName("horizontalScrollBar")
self.play = QtWidgets.QPushButton(self.centralwidget)
self.play.setGeometry(QtCore.QRect(10, 410, 75, 23))
self.play.setObjectName("play")
self.select = QtWidgets.QPushButton(self.centralwidget)
self.select.setGeometry(QtCore.QRect(550, 410, 75, 23))
self.select.setObjectName("select")
self.play.clicked.connect(self.load_video)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def load_video(self):
filepath = 'F:/Fun/Songs/12.mp4'
self.Video_Widget.load(filepath)
self.horizontalScrollBar.setMediaObject(self.Video_Widget.mediaObject())
self.Video_Widget.play()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.play.setText(_translate("MainWindow", "play"))
self.select.setText(_translate("MainWindow", "Select"))
from PyQt5.QtMultimediaWidgets import QVideoWidget
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_VideoWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
我也使用了官方文档中的这段代码。
def load_video(self):
player = QMediaPlayer()
playlist = QMediaPlaylist(player)
playlist.addMedia(QUrl('F:/Fun/Songs/12.mp4'))
player.setVideoOutput(self.Video_Widget)
self.Video_Widget.show()
playlist.setCurrentIndex(1)
player.play()
同时使用官方文档中的此代码。发生此错误。
正如错误和文档指出的那样,必须将 QMediaContent 传递给 addMedia 方法。要构建 QMediaContent,您可以使用 QUrl:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_VideoWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(733, 505)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.Video_Widget = QVideoWidget(self.centralwidget)
self.Video_Widget.setGeometry(QtCore.QRect(0, 0, 711, 361))
self.Video_Widget.setObjectName("Video_Widget")
self.horizontalScrollBar = QtWidgets.QScrollBar(self.centralwidget)
self.horizontalScrollBar.setGeometry(QtCore.QRect(0, 380, 721, 16))
self.horizontalScrollBar.setOrientation(QtCore.Qt.Horizontal)
self.horizontalScrollBar.setObjectName("horizontalScrollBar")
self.play = QtWidgets.QPushButton(self.centralwidget)
self.play.setGeometry(QtCore.QRect(10, 410, 75, 23))
self.play.setObjectName("play")
self.select = QtWidgets.QPushButton(self.centralwidget)
self.select.setGeometry(QtCore.QRect(550, 410, 75, 23))
self.select.setObjectName("select")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.play.setText(_translate("MainWindow", "play"))
self.select.setText(_translate("MainWindow", "Select"))
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5 import QtMultimedia
class VideoWindow(QtWidgets.QMainWindow, Ui_VideoWindow):
def __init__(self, parent=None):
super(VideoWindow, self).__init__(parent)
self.setupUi(self)
self.player = QtMultimedia.QMediaPlayer(self)
self.player.setVideoOutput(self.Video_Widget)
self.play.clicked.connect(self.player.play)
self.playlist = QtMultimedia.QMediaPlaylist(self.player)
self.player.setPlaylist(self.playlist)
self.playlist.addMedia(
QtMultimedia.QMediaContent(QtCore.QUrl.fromLocalFile("F:/Fun/Songs/12.mp4"))
)
self.playlist.setCurrentIndex(0)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = VideoWindow()
w.show()
sys.exit(app.exec_())
我有一个关于pyqt5的问题。我想制作一个视频播放器。现在我被困住了,我怎么能给他们完整的路径而不是点击按钮并转到路径和 select 视频我只想要它这个文件路径。请看看我的代码。我为 raspberry pi 和 window
做了这个from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_VideoWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(733, 505)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.Video_Widget = QVideoWidget(self.centralwidget)
self.Video_Widget.setGeometry(QtCore.QRect(0, 0, 711, 361))
self.Video_Widget.setObjectName("Video_Widget")
self.horizontalScrollBar = QtWidgets.QScrollBar(self.centralwidget)
self.horizontalScrollBar.setGeometry(QtCore.QRect(0, 380, 721, 16))
self.horizontalScrollBar.setOrientation(QtCore.Qt.Horizontal)
self.horizontalScrollBar.setObjectName("horizontalScrollBar")
self.play = QtWidgets.QPushButton(self.centralwidget)
self.play.setGeometry(QtCore.QRect(10, 410, 75, 23))
self.play.setObjectName("play")
self.select = QtWidgets.QPushButton(self.centralwidget)
self.select.setGeometry(QtCore.QRect(550, 410, 75, 23))
self.select.setObjectName("select")
self.play.clicked.connect(self.load_video)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def load_video(self):
filepath = 'F:/Fun/Songs/12.mp4'
self.Video_Widget.load(filepath)
self.horizontalScrollBar.setMediaObject(self.Video_Widget.mediaObject())
self.Video_Widget.play()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.play.setText(_translate("MainWindow", "play"))
self.select.setText(_translate("MainWindow", "Select"))
from PyQt5.QtMultimediaWidgets import QVideoWidget
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_VideoWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
我也使用了官方文档中的这段代码。
def load_video(self):
player = QMediaPlayer()
playlist = QMediaPlaylist(player)
playlist.addMedia(QUrl('F:/Fun/Songs/12.mp4'))
player.setVideoOutput(self.Video_Widget)
self.Video_Widget.show()
playlist.setCurrentIndex(1)
player.play()
同时使用官方文档中的此代码。发生此错误。
正如错误和文档指出的那样,必须将 QMediaContent 传递给 addMedia 方法。要构建 QMediaContent,您可以使用 QUrl:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_VideoWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(733, 505)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.Video_Widget = QVideoWidget(self.centralwidget)
self.Video_Widget.setGeometry(QtCore.QRect(0, 0, 711, 361))
self.Video_Widget.setObjectName("Video_Widget")
self.horizontalScrollBar = QtWidgets.QScrollBar(self.centralwidget)
self.horizontalScrollBar.setGeometry(QtCore.QRect(0, 380, 721, 16))
self.horizontalScrollBar.setOrientation(QtCore.Qt.Horizontal)
self.horizontalScrollBar.setObjectName("horizontalScrollBar")
self.play = QtWidgets.QPushButton(self.centralwidget)
self.play.setGeometry(QtCore.QRect(10, 410, 75, 23))
self.play.setObjectName("play")
self.select = QtWidgets.QPushButton(self.centralwidget)
self.select.setGeometry(QtCore.QRect(550, 410, 75, 23))
self.select.setObjectName("select")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.play.setText(_translate("MainWindow", "play"))
self.select.setText(_translate("MainWindow", "Select"))
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5 import QtMultimedia
class VideoWindow(QtWidgets.QMainWindow, Ui_VideoWindow):
def __init__(self, parent=None):
super(VideoWindow, self).__init__(parent)
self.setupUi(self)
self.player = QtMultimedia.QMediaPlayer(self)
self.player.setVideoOutput(self.Video_Widget)
self.play.clicked.connect(self.player.play)
self.playlist = QtMultimedia.QMediaPlaylist(self.player)
self.player.setPlaylist(self.playlist)
self.playlist.addMedia(
QtMultimedia.QMediaContent(QtCore.QUrl.fromLocalFile("F:/Fun/Songs/12.mp4"))
)
self.playlist.setCurrentIndex(0)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = VideoWindow()
w.show()
sys.exit(app.exec_())