Как в PyQt5 QtMultimedia воспроизвести видео файлы в плейлисте случайным образом или с очередностью

Данный участок кода в бесконечном цикле проигрывает один и тот же файл в папке. Хотя по задумке должен проигрывать все, что лежит в папке в бесконечном цикле. Как правильно сделать это?

main.py

import sys
from PyQt5 import QtCore
from PyQt5.QtMultimedia import QMediaPlayer, QMediaPlaylist, QMediaContent
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5.uic import loadUi
import glob
import random
import os


class New(QDialog):
    def __init__(self):
        super(New, self).__init__()

        loadUi("video_form.ui", self)

        self.player = QMediaPlayer(None, QMediaPlayer.VideoSurface)
        self.playlist = QMediaPlaylist()


    def my_video(self):
        list = [1, 2]
        video_path = 'D:/путь к папке с видео'

        mmm = random.choice(list)
        for filename in glob.glob(os.path.join(video_path, f'{mmm}.mp4')):
            self.playlist.addMedia(QMediaContent(QtCore.QUrl.fromLocalFile(filename)))
            self.player.setVideoOutput(self.VIDEO_widget)
            self.playlist.setCurrentIndex(1)
            self.playlist.setPlaybackMode(QMediaPlaylist.CurrentItemInLoop)
            self.player.setPlaylist(self.playlist)
            self.player.play()


    def closeEvent(self, e):
        super().closeEvent(e)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = New()
    window.my_video()
    window.show()
    sys.exit(app.exec_())

video_form.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>1024</width>
    <height>620</height>
   </rect>
  </property>
  <property name="minimumSize">
   <size>
    <width>1024</width>
    <height>620</height>
   </size>
  </property>
  <property name="maximumSize">
   <size>
    <width>1024</width>
    <height>620</height>
   </size>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <property name="styleSheet">
   <string notr="true">background-color: rgb(85, 170, 255);</string>
  </property>
  <property name="sizeGripEnabled">
   <bool>false</bool>
  </property>
  <property name="modal">
   <bool>false</bool>
  </property>
  <widget class="QGroupBox" name="groupBox_7">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>10</y>
     <width>1000</width>
     <height>600</height>
    </rect>
   </property>
   <property name="minimumSize">
    <size>
     <width>1000</width>
     <height>600</height>
    </size>
   </property>
   <property name="maximumSize">
    <size>
     <width>700</width>
     <height>600</height>
    </size>
   </property>
   <property name="styleSheet">
    <string notr="true">
background-color: qlineargradient(spread:reflect, x1:0, y1:1, x2:0.492, y2:1, stop:0.0197044 rgba(0, 0, 93, 218), stop:1 rgba(255, 255, 255, 255));</string>
   </property>
   <property name="title">
    <string>ххх</string>
   </property>
   <widget class="QVideoWidget" name="VIDEO_widget" native="true">
    <property name="geometry">
     <rect>
      <x>190</x>
      <y>60</y>
      <width>640</width>
      <height>480</height>
     </rect>
    </property>
    <property name="sizePolicy">
     <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
      <horstretch>0</horstretch>
      <verstretch>0</verstretch>
     </sizepolicy>
    </property>
    <property name="minimumSize">
     <size>
      <width>640</width>
      <height>480</height>
     </size>
    </property>
    <property name="maximumSize">
     <size>
      <width>640</width>
      <height>480</height>
     </size>
    </property>
    <property name="styleSheet">
     <string notr="true">background-color: rgb(0, 0, 0);</string>
    </property>
   </widget>
  </widget>
 </widget>
 <customwidgets>
  <customwidget>
   <class>QVideoWidget</class>
   <extends>QWidget</extends>
   <header>PyQt5.QtMultimediaWidgets</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

Ответы (1 шт):

Автор решения: Владимир

main.py

import sys
from PyQt5 import QtCore
from PyQt5.QtMultimedia import QMediaPlayer, QMediaPlaylist, QMediaContent
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5.uic import loadUi


class New(QDialog):
    def __init__(self):
        super(New, self).__init__()

        loadUi("video_form.ui", self)

        self.player = QMediaPlayer(None, QMediaPlayer.VideoSurface)
        self.playlist = QMediaPlaylist()

    def my_video1(self):
         self.playlist.addMedia(
             QMediaContent(QtCore.QUrl.fromLocalFile('D:\\путь\\1.mp4')))
         self.playlist.addMedia(
             QMediaContent(QtCore.QUrl.fromLocalFile('D:\\путь\\2.mp4')))
         self.playlist.addMedia(
             QMediaContent(QtCore.QUrl.fromLocalFile('D:\\путь\\3.mp4')))

         self.player.setVideoOutput(self.VIDEO_widget)
         self.playlist.setPlaybackMode(QMediaPlaylist.Random)
         self.player.setPlaylist(self.playlist)
         self.player.play()

    def closeEvent(self, e):
        super().closeEvent(e)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = New()
    window.my_video1()
    window.show()
    sys.exit(app.exec_())

video_form.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>1024</width>
    <height>620</height>
   </rect>
  </property>
  <property name="minimumSize">
   <size>
    <width>1024</width>
    <height>620</height>
   </size>
  </property>
  <property name="maximumSize">
   <size>
    <width>1024</width>
    <height>620</height>
   </size>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <property name="styleSheet">
   <string notr="true">background-color: rgb(85, 170, 255);</string>
  </property>
  <property name="sizeGripEnabled">
   <bool>false</bool>
  </property>
  <property name="modal">
   <bool>false</bool>
  </property>
  <widget class="QGroupBox" name="groupBox_7">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>10</y>
     <width>1000</width>
     <height>600</height>
    </rect>
   </property>
   <property name="minimumSize">
    <size>
     <width>1000</width>
     <height>600</height>
    </size>
   </property>
   <property name="maximumSize">
    <size>
     <width>700</width>
     <height>600</height>
    </size>
   </property>
   <property name="styleSheet">
    <string notr="true">
background-color: qlineargradient(spread:reflect, x1:0, y1:1, x2:0.492, y2:1, stop:0.0197044 rgba(0, 0, 93, 218), stop:1 rgba(255, 255, 255, 255));</string>
   </property>
   <property name="title">
    <string>ххх</string>
   </property>
   <widget class="QVideoWidget" name="VIDEO_widget" native="true">
    <property name="geometry">
     <rect>
      <x>190</x>
      <y>60</y>
      <width>640</width>
      <height>480</height>
     </rect>
    </property>
    <property name="sizePolicy">
     <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
      <horstretch>0</horstretch>
      <verstretch>0</verstretch>
     </sizepolicy>
    </property>
    <property name="minimumSize">
     <size>
      <width>640</width>
      <height>480</height>
     </size>
    </property>
    <property name="maximumSize">
     <size>
      <width>640</width>
      <height>480</height>
     </size>
    </property>
    <property name="styleSheet">
     <string notr="true">background-color: rgb(0, 0, 0);</string>
    </property>
   </widget>
  </widget>
 </widget>
 <customwidgets>
  <customwidget>
   <class>QVideoWidget</class>
   <extends>QWidget</extends>
   <header>PyQt5.QtMultimediaWidgets</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

Можно применить (QMediaPlaylist.Loop) и в бесконечном цикле проигрывать плейлист

→ Ссылка