Данная программа имеет 2 видео виджета и 2 радио кнопки. В программе указываем пути к четырем видео файлам или изображениям.
Цель данной программы проигрывать файл №1 и №3 при нажатой кнопке RB_1
, а при RB_2
- файлы №2 и №4.
Однако, программа проигрывает файлы как попало. И еще, RB_1
я сделал активной, чтобы при старте программы автоматически запустились файлы, но чего-то не хватает для запуска.
main.py
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>