Ian Wetherbee Ian Wetherbee - 7 months ago 68
Python Question

VLC Python EventManager callback type?

I'm having trouble attaching an event handler to tell when a song has finished playing when using the VLC Python bindings. The event_attach function is complaining about the callback type.

def __init__(self):
self.vlc = vlc.Instance()
self.vlc_playlist = self.vlc.media_list_new()
self.vlc_player = self.vlc.media_list_player_new()
self.vlc_player.set_media_list(self.vlc_playlist)
self.vlc_events = self.vlc_player.event_manager()
self.vlc_events.event_attach(vlc.EventType.MediaPlayerEndReached, self.SongFinished, 1)
....
def SongFinished(self, *args, **kwargs):
pass


Errors:

self.vlc_events.event_attach(vlc.EventType.MediaPlayerEndReached, self.SongFinished, 1)
return libvlc_event_attach(self, i_event_type, f_callback, user_data)
ArgumentError: argument 3: <type 'exceptions.TypeError'>: wrong type

Answer

Found it, VLC.py includes a small decorator ctypes function for wrapping callbacks:

callbackmethod=ctypes.CFUNCTYPE(None, Event, ctypes.c_void_p)

To use:

@callbackmethod
def SongFinished(self, data):
    print data

.event_attach(vlc.EventType.MediaPlayerEndReached, SongFinished, 1)