Python VLC Bindings Event Example

This forum is about all development around libVLC.
JeffHoogland
New Cone
New Cone
Posts: 4
Joined: 21 Dec 2012 17:28
Operating System: Linux

Python VLC Bindings Event Example

Postby JeffHoogland » 21 Dec 2012 17:34

I'm using the VLC python bindings found here -> http://wiki.videolan.org/Python_bindings

I've also been reading over the excellent API documentation found here -> http://liris.cnrs.fr/advene//download/p ... types/doc/

The thing that is currently escaping me though - how do I have an event fire when a song ends? I'm assuming it has to do with EventType.MediaPlayerEndReached - but could anyone out there provide a small example on how to utilize this event? The proper syntax escapes me.

~Jeff

JeffHoogland
New Cone
New Cone
Posts: 4
Joined: 21 Dec 2012 17:28
Operating System: Linux

Re: Python VLC Bindings Event Example

Postby JeffHoogland » 21 Dec 2012 21:11

For reference a small bit of code I am trying that is failing:

Code: Select all

>>> mymedia = MediaList() >>> mymedia.add_media("/path/to/mediafile1") 0 >>> mymedia.add_media("/path/to/mediafile2") 0 >>> player = MediaListPlayer() >>> player.set_media_list(mymedia) >>> pevent = player.event_manager() >>> def event_cb( ev ): print "Hey look an event..." >>> pevent.event_attach(EventType().MediaPlayerMediaChanged, event_cb) -1
When I tell my player to play the media comes up just fine - but the event doesn't fire when I change tracks. I'm assuming it has to do with the returning -1 from the event_attach - what am I doing wrong?

~Jeff

geoffs
New Cone
New Cone
Posts: 3
Joined: 18 Apr 2011 22:28

Re: Python VLC Bindings Event Example

Postby geoffs » 02 Jan 2013 00:20

You're right that the -1 returned by event_attach is a problem. That's telling you that object doesn't send that event. In the version of libvlc I have, MediaListPlayer only sends EventType.MediaListPlayerNextItemSet. Try using that event.

If that doesn't do what you need, or you also want to listen for some of the MediaPlayer events, you can create a MediaPlayer, set that inside the MediaListPlayer and listen for events on the MediaPlayer as well. Something like this:

Code: Select all

from vlc import * mlp = MediaListPlayer() mp = MediaPlayer() mlp.set_media_player(mp) def cb(event): print "cb:", event.type, event.u mlp_em = mlp.event_manager() mlp_em.event_attach(EventType.MediaListPlayerNextItemSet, cb) mp_em = mp.event_manager() mp_em.event_attach(EventType.MediaPlayerEndReached, cb) mp_em.event_attach(EventType.MediaPlayerMediaChanged, cb) ml = MediaList() ml.add_media("/path/to/mediafile1") ml.add_media("/path/to/mediafile2") mlp.set_media_list(ml) mlp.play()
You probably don't need all 3 of those events to do what you want. This is just an example.

Once you're receiving events, look at the event.u field which is an EventUnion to get the data from the event that you want. You may have to dig around a bit in the libvlc code to figure out what data is sent by which event. I'm not sure if that's documented anywhere.

JeffHoogland
New Cone
New Cone
Posts: 4
Joined: 21 Dec 2012 17:28
Operating System: Linux

Re: Python VLC Bindings Event Example

Postby JeffHoogland » 02 Jan 2013 00:24

Thanks a lot for the example! I actually got it working yesterday (finally!) by looking at the event attach example in the vlc.py

For any who care I am using the VLC libs with an application I am working on -> http://jeffhoogland.blogspot.com/2013/0 ... lient.html

~Jeff

panic82
New Cone
New Cone
Posts: 8
Joined: 01 Sep 2016 19:15

Re: Python VLC Bindings Event Example

Postby panic82 » 03 Sep 2016 13:39

Thanks Jeff and Geoff. Jeff your example code has been very helpful and Geoff yours has solved an issue I was having related to events with the MediaListPlayer. In case anyone is curious:

Previously I only had an instance of vlc_player but after wrapping the instance of MediaPlayer with it, I was able to attach to events on MediaPlayer:

Code: Select all

main_player = vlc_instance.media_player_new() vlc_player = vlc_instance.media_list_player_new() vlc_player.set_media_player(main_player) vlc_events = main_player.event_manager() #print("State:" + str(vlc_player.get_state())) def vlc_action_occurred(event): print('BING BONG **************************************') return def vlc_time_changed(self, player): print print 'FPS =', player.get_fps() print 'time =', player.get_time(), '(ms)' print 'FRAME =', .001 * player.get_time() * player.get_fps() return vlc_events.event_attach(vlc.EventType.MediaPlayerMediaChanged, vlc_action_occurred) vlc_events.event_attach(vlc.EventType.MediaPlayerTimeChanged, vlc_time_changed, main_player) vlc_events.event_attach(vlc.EventType.MediaPlayerPlaying, vlc_action_occurred) vlc_events.event_attach(vlc.EventType.MediaPlayerBuffering, vlc_action_occurred) media_list = vlc_instance.media_list_new() for item in screen: media_item = vlc_instance.media_new(item['url']) media_list.add_media(media_item) item['vlc_index'] = media_list.index_of_item(media_item) print(item['vlc_index']) vlc_player.set_media_list(media_list)

Calling vlc_player stop and start now cause events to trigger!

kartik
New Cone
New Cone
Posts: 1
Joined: 11 Oct 2019 19:26

Python VLC Bindings instance

Postby kartik » 11 Oct 2019 19:46

Hi all, I am using the vlc instance to play a list of songs in python as follows

instance = vlc.Instance()
player = instance.media_list_player_new()
media_list = instance.media_list_new(list_of_song)
player.set_media_list(media_list)

All songs are played one by one until the end of the song list. But I am unable to identify the end of a song and start of the next. Please Help to identify this.


Return to “Development around libVLC”

Who is online

Users browsing this forum: No registered users and 31 guests