Python VLC binding- playing a playlist

Natko Kraševac picture Natko Kraševac · Feb 10, 2015 · Viewed 9k times · Source

I was wondering if it is possible to open(play) a music playlist ( .m3u file) with the use of vlc.py ? I searched for an answer but could not find it. I managed to play a simple mp3 file, even a mp3 stream, but I dont have any luck with the playlists. Can you help me, give me some example code? I want to be able to go through tracks( Next and Previous) within my python program. Thans in advance

Answer

Rolf of Saxony picture Rolf of Saxony · Oct 9, 2015

Here is a "very" rough mock up of some code that I wrote for something else, adapted to your question.
It should allow you, using vlc.py, to play streamed audio, an m3u audio playlist and an mp3 file.
As I said it is very rough code but it should point you in the right direction.
Hope it helps.

import requests
import vlc
from time import sleep
urls = [
    'http://network.absoluteradio.co.uk/core/audio/aacplus/live.pls?service=acbb',
    'file:///home/rolf/test.m3u',
    'file:///home/rolf/happy.mp3',
    'http://statslive.infomaniak.ch/playlist/energy90s/energy90s-high.mp3/playlist.pls',
    'http://streaming.radio.rtl2.fr/rtl2-1-44-128',
    ]

playlists = set(['pls','m3u'])

Instance = vlc.Instance()

for url in urls:
    ext = (url.rpartition(".")[2])[:3]
    test_pass = False    
    try:
        if url[:4] == 'file':
            test_pass = True
        else:
            r = requests.get(url, stream=True)
            test_pass = r.ok
    except Exception as e:
        print('failed to get stream: {e}'.format(e=e))
        test_pass = False
    else:
        if test_pass:
            print('Sampling for 15 seconds')
            player = Instance.media_player_new()
            Media = Instance.media_new(url)
            Media_list = Instance.media_list_new([url])
            Media.get_mrl()
            player.set_media(Media)
            if ext in playlists:
                list_player = Instance.media_list_player_new()
                list_player.set_media_list(Media_list)
                if list_player.play() == -1:
                    print ("Error playing playlist")
            else:
                if player.play() == -1:
                    print ("Error playing Stream")
            sleep(15)
            if ext in playlists:
                list_player.stop()
            else:
                player.stop()

        else:
            print('error getting the audio')