Search code examples
vlclibvlc

Using libVLC media_list instead of a media_player to play a directory of songs


I'm trying to use libVLC v2 C bindings in order to play all the songs (mp3/m4a/ogg)s inside a directory given its path.

I'm currently only using the module libvlc_media_player, with methods like libvlc_media_player_set_media to set a song from a given path.

I see there is a module called libvlc_media_list, with libvlc_media_list_set_media.

What is a libvlc_media_list and how do I set it with a path to a directory (with several audio files inside)? The libvlc_media_list_player takes a libvlc_media_player, but I do not know where to set the media (path).


Solution

  • media_list is used to play play lists(.pls .m3u etc) as opposed to individual files.
    Not sure about c but in python:

    Media_list = Instance.media_list_new([url])
    list_player = Instance.media_list_player_new()
    list_player.set_media_list(Media_list)
    list_player.play()
    

    as opposed to:

    player = Instance.media_player_new()
    Media = Instance.media_new(url)
    Media.get_mrl()
    player.set_media(Media)
    player.play()
    

    for an individual file.
    I hope that you can pick the bones out of the above.
    For your purposes, it looks like you need to use the individual file option, using a url list, whilst looping over the list.

    Again (apologies) in python:

    import vlc
    import time
    my_list = ['vp1.mp3','happy.mp3']
    instance = vlc.Instance()
    player = instance.media_player_new()
    playing = set([1,2,3,4])
    for i in my_list:
        player.set_mrl(i)
        player.play()
        play=True
        while play == True:
            time.sleep(1)
            play_state = player.get_state()
            if play_state in playing:
                continue
            else:
                play = False