1

I'm trying to play a video using VLC, and close the window after the video is finished. However, I can't close the player window. I tried releasing instances of the player and media, but it doesn't work. And I couldn't find anything else in the API documentation.

Note that I don't want to terminate the whole application after the player finished, so sys.exit is not an option.

The following code is what I'm doing.

import vlc

VIDEO_PATH="/path/to/video.mp4"

def get_end_callback(mediaplayer):
    def end_callback(event):
        print("End of playing reached")
        mediaplayer.stop()
        mediaplayer.get_media().release()
        mediaplayer.release()
        mediaplayer.get_instance().release()
    return end_callback

def play():
    vlc_instance = vlc.Instance(["--no-xlib"])
    media_player = vlc.MediaPlayer(vlc_instance, VIDEO_PATH)

    media_player.play()

    event_manager = media_player.event_manager()
    event_manager.event_attach(vlc.EventType.MediaPlayerEndReached, get_end_callback(media_player))

play()

input("press Enter to exit")

I'm testing it with python=3.9, python-vlc=3.0.12118, vlc=3.0.9.2, on Ubuntu=20.04. I also tried another machine with older OS and older VLC.

Aidin
  • 1,271
  • 14
  • 20
  • If you can't find anything with those libraries, maybe try one or the other option here: https://stackoverflow.com/a/27712018/42346 – mechanical_meat Nov 17 '21 at 04:01

1 Answers1

0

So LibVLC will spawn a window to draw on, if you don't provide one. You don't want that, as you won't be able to manage it easily.

Before calling media_player.play(), you want to call media_player.set_hwnd(window.handle) (for Windows) / media_player.set_xwindow(window.xid) (for Linux) with a window handle.

Creating and managing and deleting this window is up to you and it does not have to be your main window of your app.

Here you can find more complete yet minimal samples with Qt and GTK frameworks https://github.com/oaubert/python-vlc/tree/master/examples

mfkl
  • 1,914
  • 1
  • 11
  • 21