该程序有 2 个视频小部件和 2 个单选按钮。在程序中,指定四个视频文件或图像的路径。
该程序的目的是在按下按钮时播放 1 号和 3 号文件RB_1
,以及在RB_2
2 号和 4 号文件时播放。
但是,该程序随机播放文件。然而,RB_1
我把它激活了,这样当程序启动时,文件会自动启动,但启动时缺少一些东西。
主文件
import sys
from PyQt5 import QtCore
from PyQt5.QtMultimedia import QMediaPlayer, QMediaPlaylist, QMediaContent
from PyQt5.QtCore import pyqtSlot
from PyQt5.Qt import Qt
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5.uic import loadUi
class New(QDialog):
def __init__(self):
super(New, self).__init__()
loadUi("RB_Video.ui", self)
self.RB_1.clicked.connect(self.func_1)
self.RB_1.setLayoutDirection(Qt.RightToLeft)
self.RB_1.setChecked(True)
self.RB_2.clicked.connect(self.func_2)
self.player = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.playlist = QMediaPlaylist()
self.player_2 = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.playlist_2 = QMediaPlaylist()
@pyqtSlot()
def func_1(self):
self.video_left()
self.video_right()
def func_2(self):
self.video_left()
self.video_right()
def video_left(self):
if self.RB_1.isChecked():
self.playlist.addMedia(
QMediaContent(QtCore.QUrl.fromLocalFile('путь к файлу №1')))
elif self.RB_2.isChecked():
self.playlist.addMedia(
QMediaContent(QtCore.QUrl.fromLocalFile('путь к файлу №2')))
self.player.setVideoOutput(self.VIDEO_widget_1)
self.playlist.setPlaybackMode(QMediaPlaylist.CurrentItemInLoop)
self.player.setPlaylist(self.playlist)
self.player.play()
def video_right(self):
if self.RB_1.isChecked():
self.playlist_2.addMedia(
QMediaContent(QtCore.QUrl.fromLocalFile('путь к файлу №3')))
elif self.RB_2.isChecked():
self.playlist_2.addMedia(
QMediaContent(QtCore.QUrl.fromLocalFile('путь к файлу №4')))
self.player_2.setVideoOutput(self.VIDEO_widget_2)
self.playlist_2.setPlaybackMode(QMediaPlaylist.CurrentItemInLoop)
self.player_2.setPlaylist(self.playlist_2)
self.player_2.play()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = New()
window.show()
sys.exit(app.exec_())
RB_Video.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1228</width>
<height>760</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QVideoWidget" name="VIDEO_widget_1" native="true">
<property name="geometry">
<rect>
<x>260</x>
<y>90</y>
<width>300</width>
<height>200</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>300</width>
<height>200</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>300</width>
<height>200</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(0, 0, 0);</string>
</property>
</widget>
<widget class="QVideoWidget" name="VIDEO_widget_2" native="true">
<property name="geometry">
<rect>
<x>610</x>
<y>90</y>
<width>300</width>
<height>200</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>300</width>
<height>200</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>300</width>
<height>200</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(0, 0, 0);</string>
</property>
</widget>
<widget class="QRadioButton" name="RB_1">
<property name="geometry">
<rect>
<x>470</x>
<y>390</y>
<width>95</width>
<height>20</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>RB_A</string>
</property>
</widget>
<widget class="QRadioButton" name="RB_2">
<property name="geometry">
<rect>
<x>630</x>
<y>390</y>
<width>95</width>
<height>20</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>RB_B</string>
</property>
</widget>
</widget>
<customwidgets>
<customwidget>
<class>QVideoWidget</class>
<extends>QWidget</extends>
<header>PyQt5.QtMultimediaWidgets</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
像这样试试