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.