0

I have some videos in a GUI that plays when certain conditions are met. The problem I have is that I am unable to make the videos start again after they end. Here is the code for the UI:

import os
from time import sleep
from PyQt5 import uic
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QMainWindow, QVBoxLayout, QWidget
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtCore import QThread, pyqtSignal, pyqtSlot, QUrl
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer, QMediaPlaylist

# For update UI
class UIThread(QThread):
    change_pixmap_signal = pyqtSignal(int)
    current_mode = None
    new_mode = None

    def run(self):
        while True:
            sleep(0.1)
            if self.current_mode != self.new_mode:
                self.current_mode = self.new_mode
                self.change_pixmap_signal.emit(self.current_mode)

# Main window class
class MainUI(QMainWindow):
    def __init__(self):
        super(MainUI, self).__init__()

        # Load UI from files
        uic.loadUi('ui/main_small.ui', self)
        self.main_label.setPixmap(QPixmap('ui/images/main.png'))
        self.eco_label.setPixmap(QPixmap('ui/images/eco.png'))
        self.ev_mode_label.setPixmap(QPixmap('ui/images/ev_mode.png'))
        self.normal_mode_label.setPixmap(QPixmap('ui/images/normal_mode.png'))
        self.power_mode_label.setPixmap(QPixmap('ui/images/power_mode.png'))
        self.power_mode('N')

        self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
        self.videoWidget = QVideoWidget()
        self.videoWidget.move(0, 0)
        self.mediaPlayer.setVideoOutput(self.videoWidget)
        self.videoWidget.show()
        self.playlist = QMediaPlaylist()

        layout = QVBoxLayout()
        layout.addWidget(self.videoWidget)
        self.video_groupBox.setLayout(layout)

        # Default values
        self.mode = None
        self.speed_label.setStyleSheet("color: white")
        self.motor1_speed_label.setStyleSheet("color: white")
        self.motor2_speed_label.setStyleSheet("color: white")
        self.motor3_speed_label.setStyleSheet("color: white")
        self.motor1_torque_label.setStyleSheet("color: white")
        self.motor2_torque_label.setStyleSheet("color: white")
        self.motor3_torque_label.setStyleSheet("color: white")

        # Create the video capture thread
        self.uithread = UIThread()
        # Connect its signal to the update_image slot
        self.uithread.change_pixmap_signal.connect(self.__update_mode)
        self.uithread.start()

    # Play videos
    def play_video(self, video):
        self.playlist = QMediaPlaylist()
        self.playlist.setPlaybackMode(QMediaPlaylist.Loop)

        if video == 'x':
            self.playlist.addMedia(QMediaContent(QUrl.fromLocalFile(os.getcwd() + '/ui/videos/video_x.mp4')))
        elif video == 'xy':
            self.playlist.addMedia(QMediaContent(QUrl.fromLocalFile(os.getcwd() + '/ui/videos/video_xy.mp4')))
        elif video == 'xyz':
            self.playlist.addMedia(QMediaContent(QUrl.fromLocalFile(os.getcwd() + '/ui/videos/video_xyz.mp4')))
        elif video == 'yz':
            self.playlist.addMedia(QMediaContent(QUrl.fromLocalFile(os.getcwd() + '/ui/videos/video_yz.mp4')))
        elif video == 'z':
            self.playlist.addMedia(QMediaContent(QUrl.fromLocalFile(os.getcwd() + '/ui/videos/video_z.mp4')))
        else:    # zzz
            self.playlist.addMedia(QMediaContent(QUrl.fromLocalFile(os.getcwd() + '/ui/videos/video_zzz.mp4')))
        
        self.mediaPlayer.setPosition(0)
        self.mediaPlayer.setPlaylist(self.playlist)
        self.mediaPlayer.play()

    # Change display mode
    def change_mode(self, mode):
        self.uithread.new_mode = mode

    # Select R N D P mode
    def rgd_select(self, select):
        self.r_label.setPixmap(QPixmap('ui/images/r_white.png'))
        self.n_label.setPixmap(QPixmap('ui/images/n_white.png'))
        self.d_label.setPixmap(QPixmap('ui/images/d_white.png'))
        self.p_label.setPixmap(QPixmap('ui/images/p_white.png'))

        if select == 'R':
            self.r_label.setPixmap(QPixmap('ui/images/r_green.png'))
        elif select == 'N':
            self.n_label.setPixmap(QPixmap('ui/images/n_green.png'))
        elif select == 'D':
            self.d_label.setPixmap(QPixmap('ui/images/d_green.png'))
        else:
            self.p_label.setPixmap(QPixmap('ui/images/p_green.png'))

    # Red power bar
    def power_bar_enable(self, enable):
        if enable:
            self.red_power_label.setHidden(False)
        else:
            self.red_power_label.setHidden(True)

    # Yellow normal mode bare
    def normal_bar_enable(self, enable):
        if enable:
            self.ev_mode_bar_label.setHidden(True)
            self.normal_mode_bar_label.setHidden(False)
        else:
            self.normal_mode_bar_label.setHidden(True)

    # EV mode bar
    def ev_bar_enable(self, enable):
        if enable:
            self.normal_mode_bar_label.setHidden(True)
            self.ev_mode_bar_label.setHidden(False)
        else:
            self.ev_mode_bar_label.setHidden(True)

    # Charging bar
    def charging_bar_enable(self, enable):
        if enable:
            self.charging_bar_label.setHidden(False)
        else:
            self.charging_bar_label.setHidden(True)

    # Selec yellow bar
    def ef_bar_select(self, full):
        if full:
            self.bar_label.setPixmap(QPixmap('ui/images/full_bar.png'))
        else:
            self.bar_label.setPixmap(QPixmap('ui/images/half_bar.png'))
            

    # READY or not
    def ready_set(self, enable):
        if enable:
            self.ready_label.setHidden(False)
            self.ready_label.setPixmap(QPixmap('ui/images/ready.png'))
        else:
            self.ready_label.setHidden(True)

    # EV Normal Power
    def power_mode(self, mode):
        self.eco_label.setHidden(True)
        self.ev_mode_label.setHidden(True)
        self.normal_mode_label.setHidden(True)
        self.power_mode_label.setHidden(True)

        if mode == 'E':
            self.eco_label.setHidden(False)
            self.ev_mode_label.setHidden(False)
        elif mode == 'N':
            self.eco_label.setHidden(False)
            self.normal_mode_label.setHidden(False)
        else:
            self.power_mode_label.setHidden(False)

    # Change battery
    def battery_update(self, percent):
        self.battery_progressBar.setValue(percent)

    @pyqtSlot(int)
    def __update_mode(self, mode):
        print(f"Change to mode {mode}")

        if mode == 0:
            self.rgd_select('N')
            self.ready_set(True)

        elif mode == 1 or mode == 2:
            self.battery_update(100)
            self.rgd_select('D')
            self.speed_label.setText("30")
            self.motor1_speed_label.setText("10")
            self.motor2_speed_label.setText("20")
            self.motor3_speed_label.setText("30")
            self.motor1_torque_label.setText("2")
            self.motor2_torque_label.setText("4")
            self.motor3_torque_label.setText("6")
            print("Play video x")
            self.play_video('x')
4
  • please reduce your program to the minimal code that still exhibits the problem
    – jsotola
    Commented Jul 13, 2023 at 23:06
  • code was reduced..
    – Shamooooot
    Commented Jul 14, 2023 at 5:16
  • I see no executable code. It defines a couple of classes.
    – Milliways
    Commented Jul 14, 2023 at 5:57
  • this is the UI code, and there's a main code that sends the mode value to this one to start a specific mode etc..
    – Shamooooot
    Commented Jul 14, 2023 at 6:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.