The main problem is that you are using the wrong method to create your instance of Media
:
-
self.instance = vlc.Instance()
creates a base vlc instance.
-
self.player = self.instance.media_player_new()
creates an empty player.
-
self.media = self.instance.media_player_new('C:/Users/.../TT T01-12.mkv')
creates another instance of the player, but this time it receives the path to play. at this point you could do self.media.play()
.
-
self.player.set_media(self.media)
: this is what causes the error, you are trying to pass as a medium to play a player another instance of Media Player .
You must use the vlc.Instance.media_new()
method to create self.media
:
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication
import vlc
import time
class Principal(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.instance = vlc.Instance()
self.player = self.instance.media_player_new()
self.media = self.instance.media_new('C:/Users/Angel/Videos/TT T01-08/TT T01-12.mkv')
self.player.set_media(self.media)
self.player.play()
app = QApplication([])
p = Principal()
p.show()
app.exec_()
I do not know why you use time.sleep
but you should never use it in a GUI, it is blocking so the interface freezes and stops responding when you block your mainloop .
The previous code is limited to playing the video in a window external to that of the Qt interface, the normal thing is that you want to integrate the player in your app. It is relatively simple to embed the player in QFrame
for example:
import sys
from PyQt5 import QtWidgets
import vlc
class Principal(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.widget = QtWidgets.QWidget(self)
self.setCentralWidget(self.widget)
self.resize(960, 540)
self.instance = vlc.Instance()
self.player = self.instance.media_player_new()
self.video_frame = QtWidgets.QFrame()
self.player.set_hwnd(self.video_frame.winId())
self.box_layout = QtWidgets.QVBoxLayout()
self.box_layout.addWidget(self.video_frame)
self.widget.setLayout(self.box_layout)
self.media = self.instance.media_new('C:/Users/Angel/Videos/TT T01-08/TT T01-12.mkv')
self.player.set_media(self.media)
self.player.play()
app = QtWidgets.QApplication([])
p = Principal()
p.show()
app.exec_()
Note: The way to connect the widget via id
with the player is platform-dependent. For linux (X) the method set_xwindow
is used, for Windows set_hwnd
and for MacOs set_nsobject