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
Asked
Active
Viewed 1.4k times
7

Michael Scott Asato Cuthbert
- 3,442
- 2
- 22
- 52

Natko Kraševac
- 111
- 1
- 2
- 8
-
Your going to have to code your own playlist system or find one thats already available. – Bioto Feb 10 '15 at 20:09
-
what do you mean my own system? VLC player (with GUI) can play .m3u files and go to the next track,previous track... I wanted to know how it can be done with vlc.py module... – Natko Kraševac Feb 10 '15 at 20:13
-
@NatkoKraševac Did you ever get this to work? – Rolf of Saxony Jan 30 '16 at 14:16
2 Answers
10
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')

Rolf of Saxony
- 21,661
- 5
- 39
- 60
5
Just change the path,your good to go..
from vlc import Instance
import time
import os
class VLC:
def __init__(self):
self.Player = Instance('--loop')
def addPlaylist(self):
self.mediaList = self.Player.media_list_new()
path = r"C:\Users\dell5567\Desktop\engsong"
songs = os.listdir(path)
for s in songs:
self.mediaList.add_media(self.Player.media_new(os.path.join(path,s)))
self.listPlayer = self.Player.media_list_player_new()
self.listPlayer.set_media_list(self.mediaList)
def play(self):
self.listPlayer.play()
def next(self):
self.listPlayer.next()
def pause(self):
self.listPlayer.pause()
def previous(self):
self.listPlayer.previous()
def stop(self):
self.listPlayer.stop()
Create a object
player = VLC()
Add playlist
player.addPlaylist()
Play the song
player.play()
time.sleep(9)
Play the next song
player.next()
time.sleep(9)
Pause the song
player.pause()
time.sleep(9)
Resume the song
player.play()
time.sleep(9)
Previous song
player.previous()
time.sleep(9)
Stop the song
player.stop()

Pramod
- 161
- 2
- 4