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.

1

1 Answer 1

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

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.