Code randomly hangs on libvlc_media_player_set_media
Posted: 07 Aug 2021 08:58
I am trying to create a slideshow style viewer which will display videos and images. However, when testing my approach (code below) the VLC randomly freezes on `self.mediaplayer.set_media(self.media)` in the `Player.play` method. Specifically using the trace module it seems to hang on line 331 in the `vlc.py` file of this library (`vlc_pyqt.py` is my code). The issue seems to be specifically with the `libvlc_media_player_set_media` method.
I also tried to print `vlc.Media.get_state()` to determine if the video/image is being buffered and therefore can not be played but experienced no such event.
Is this a bug or am I doing something wrong? Thanks beforehand for your help everyone!
Trace output:
Code:
I am using the latest version of both the `python-vlc` library and VLC itself available to RPi 4.
I also tried to print `vlc.Media.get_state()` to determine if the video/image is being buffered and therefore can not be played but experienced no such event.
Is this a bug or am I doing something wrong? Thanks beforehand for your help everyone!
Trace output:
Code: Select all
vlc_pyqt.py(61): self.mediaplayer.set_media(self.media)
--- modulename: vlc, funcname: set_media
vlc.py(3467): return libvlc_media_player_set_media(self, p_md)
--- modulename: vlc, funcname: libvlc_media_player_set_media
vlc.py(6272): f = _Cfunctions.get('libvlc_media_player_set_media', None) or \
vlc.py(6275): return f(p_mi, p_md)
--- modulename: vlc, funcname: from_param
vlc.py(329): if this is None:
vlc.py(331): return this._as_parameter_
--- modulename: vlc, funcname: from_param
vlc.py(329): if this is None:
vlc.py(331): return this._as_parameter_
Code: Select all
# Based on https://github.com/oaubert/python-vlc/blob/master/examples/pyqt5vlc.py
import platform
import sys
from PyQt5.QtCore import Qt
from PyQt5 import QtWidgets, QtTest, QtGui, QtCore
import vlc
class Player(QtWidgets.QMainWindow):
"""A simple Media Player using VLC and Qt"""
def __init__(self, master=None):
QtWidgets.QMainWindow.__init__(self, master)
self.setWindowTitle("Media Player")
self.setCursor(Qt.BlankCursor)
self.instance = vlc.Instance() # create a basic vlc instance
self.mediaplayer = self.instance.media_player_new() # create an empty vlc media player
self.media = None
self.create_ui()
def create_ui(self):
"""Set up the user interface"""
self.widget = QtWidgets.QWidget(self)
self.setCentralWidget(self.widget)
# In this widget, the video will be drawn
self.videoframe = QtWidgets.QFrame()
self.vboxlayout = QtWidgets.QVBoxLayout(self.widget)
# self.vboxlayout.addSpacing(0)
self.vboxlayout.addWidget(self.videoframe)
self.widget.setLayout(self.vboxlayout)
self.widget.layout().setSpacing(0)
self.widget.layout().setContentsMargins(0, 0, 0, 0)
# The media player has to be 'connected' to the QFrame (otherwise the
# video would be displayed in it's own window). This is platform
# specific, so we must give the ID of the QFrame (or similar object) to
# vlc. Different platforms have different functions for this
if platform.system() == "Linux": # for Linux using the X Server
self.mediaplayer.set_xwindow(int(self.videoframe.winId()))
elif platform.system() == "Windows": # for Windows
self.mediaplayer.set_hwnd(int(self.videoframe.winId()))
elif platform.system() == "Darwin": # for MacOS
self.mediaplayer.set_nsobject(int(self.videoframe.winId()))
def play(self, media_path, display_time_ms=None):
self.media = self.instance.media_new(media_path)
print(self.media.get_state())
self.media.parse() # parse the metadata of the file
print(self.media.get_state())
self.mediaplayer.set_media(self.media)
self.mediaplayer.play()
if not display_time_ms:
QtTest.QTest.qWait(self.media.get_duration())
else:
QtTest.QTest.qWait(display_time_ms)
def main():
"""Entry point for our simple vlc player"""
app = QtWidgets.QApplication(sys.argv)
player = Player()
player.showFullScreen()
while True:
player.play("/data/demo_1080p.mp4")
player.play("/data/demo_1080p.mp4")
player.play("/data/demo_1080p.mp4", display_time_ms=4000)
player.play("/data/bam2.jpg", display_time_ms=4000)
player.play("/data/dat.png", display_time_ms=4000)
player.play("/data/kino.jpg", display_time_ms=4000)
player.play("/data/demo_720p.mp4", display_time_ms=4000)
player.play("/data/kino.jpg", display_time_ms=4000)
sys.exit(app.exec_())
if __name__ == "__main__":
main()