Sachin Acharya
Sachin Acharya

Reputation: 83

How to add event and handle playback in vlc-python

In the following code

# python-vlc
from vlc import Media, MediaPlayer, EventType, Event

class MusicPlayer:
    def __init__(self) -> None:
        self.items = ["/list/of/path/to/audio/file"]
        self.index = 0
        self.player: MediaPlayer = MediaPlayer(self.items[self.index])
        event_manager = self.player.event_manager()
        event_manager.event_attach(EventType.MediaPlayerEndReached, self.handleLoop)
    def change(self):
        self.player.stop()
        self.player.set_media(Media(self.items[self.index]))
        self.player.play()
    def play(self):
        self.player.play()
    def handleLoop(self, event):
        self.index += 1
        print('Play Ended')
        self.change(self.index)

When the media play is over the handleLoop is supposed to get triggered and it does but when it get triggered only these two lines get executed, self.index += 1 and print('Play Ended'). self.change(self.index) method doesn't gets executed. I am not even get any error. I tried attaching event for error and still got nothing.

Do anyone knows solution to that?

Upvotes: 0

Views: 417

Answers (1)

Lupilum
Lupilum

Reputation: 386

This happens most likely because libvlc is not reentrant. This means that the state of the player cannot be changed in the callback function or any function which it calls. Changes to the player must instead happen on the main thread.

Note on event_attach:

Note: The callback function must have at least one argument, an Event instance. Any other, optional positional and keyword arguments are in addition to the first one. Warning: libvlc is not reentrant, i.e. you cannot call libvlc functions from an event handler. They must be called from the main application thread.

Upvotes: 0

Related Questions