1

I am writing a script that, when launched, plays a video by its number. Then, after the end of playback, the script asks for the video number for the next playback, and so on. I use VLC python in PyQT5.


import sys
import time
import vlc

from PyQt5 import QtGui, QtWidgets
from PyQt5.QtCore import Qt


class Player(QtWidgets.QMainWindow):
    
    
    def __init__(self, parent=None):
        super(Player, self).__init__(parent)
        self.setWindowTitle("Media Player")
        # creating a basic vlc instance
        self.instance = vlc.Instance(['--video-on-top'])
        self.instance.log_unset()
        
        self.media=None
        
        self.mediaplayer = self.instance.media_player_new()
        
        self.create_ui()
        self.open_file()
    def create_ui(self):
        
        
        self.videoframe = QtWidgets.QFrame(
            frameShape=QtWidgets.QFrame.Box, frameShadow=QtWidgets.QFrame.Raised)

        if sys.platform.startswith("linux"):  # for Linux using the X Server
            self.mediaplayer.set_xwindow(self.videoframe.winId())
        elif sys.platform == "win32":  # for Windows
            self.mediaplayer.set_hwnd(self.videoframe.winId())
        elif sys.platform == "darwin":  # for MacOS
            self.mediaplayer.set_nsobject(self.videoframe.winId())

        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)
        self.setWindowFlags(Qt.FramelessWindowHint)
        lay = QtWidgets.QVBoxLayout(central_widget)
        lay.addWidget(self.videoframe)
        
    def play(self):
        if self.mediaplayer.play() == -1:
           self.open_file()
        self.mediaplayer.play()
    
    def open_file(self):
        numberfile = input("Number file: ")
        filename = (f"/home/Tadont/Video/{numberfile}.mp4")
        self.media = self.instance.media_new(filename)
        self.mediaplayer.set_media(self.media)
        self.eventManager=self.mediaplayer.event_manager()
        self.eventManager.event_attach(vlc.EventType.MediaPlayerEndReached, self.next_file)
        self.play()
        
      
    def next_file(self, event):
         if event.type == vlc.EventType.MediaPlayerEndReached:
             #self.mediaplayer.stop()
             self.open_file()
        
        

def main():   
   app = QtWidgets.QApplication(sys.argv)
   player = Player()
   player.show()
   player.move(1, 1)
   player.resize(406, 86)
   player.setObjectName("MainWindow")
   player.setStyleSheet("#MainWindow{background-color:black}")
   sys.exit(app.exec_())


if __name__ == "__main__":
   main()

I have the first file to play, then I write the file number for the second playback, but the second file does not start. How to solve this problem?

I guess I don't have a new

self.media

1 Answer 1

0

It's because the callback for the VLC MediaPlayerEndReached event will be run on a worker thread, not on the main thread. So, use the inter-thread signal mechanism or the postEvent().

This is an example for the latter.

...
from PyQt5.QtCore import QCoreApplication, QEvent
...
class Player(QtWidgets.QMainWindow):
    ...
    def open_file(self):
        ....
        self.eventManager=self.mediaplayer.event_manager()
        self.eventManager.event_attach(vlc.EventType.MediaPlayerEndReached,
            lambda _: QCoreApplication.postEvent(self, QEvent(QEvent.User))
        self.play()

    def customEvent(self, event):
        # You need to dispatch if you use custom events for other cases.
        self.open_file()
...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.