Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python VLC binding- playing a playlist

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

like image 620
Natko Kraševac Avatar asked Feb 10 '15 20:02

Natko Kraševac


2 Answers

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')
like image 57
Rolf of Saxony Avatar answered Sep 22 '22 00:09

Rolf of Saxony


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()
like image 24
Pramod Avatar answered Sep 22 '22 00:09

Pramod