0

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?

1
  • 1
    It looks like you're passing self.index as an argument for self.change() when it only takes the argument self. You should probably be getting an error, but try removing that anyway. Commented Mar 13, 2023 at 18:22

1 Answer 1

0

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.

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.