Python VLC – Next track

  python, raspberry-pi4, vlc

I am writing a program in Python to run on a Raspberry Pi in order to control my Wurlitzer jukebox. The program current accepts the codes for "record selection" (A1, B1, C4, etc.), add those codes to a playlist, and executes the list. My issue is that once a song starts, I would like to be able to press a button ("Y" in the current code) to skip the currently playing song. I can’t get this to work.

If I use "player.next()" I get an error: ‘MediaPlayer’ object has no attribute ‘next’.

I tried to stop the player and restart it (thinking it would pick up the next song in the Playlist. This doesn’t even stop the player.

I do not want to use subprocess if I can avoid it. I’d like to figure out a way within Python to do the skipping. How would one accomplish this?

import os, sys, csv, vlc, time, threading
from pynput.keyboard import Key, Listener

DefaultUSBPath="/media/pi"
PlayHistory="PlayHistory.csv"

#
# Declare variables
#
USBDrive = None
Action = None
Playlist = []
SelectionCount = []
Sel_char = None
#
# Find the USB Drive
#
for item in os.listdir(DefaultUSBPath):
    if os.path.isdir(os.path.join(DefaultUSBPath, item)):
        if USBDrive is None:
            USBDrive =  os.path.join(DefaultUSBPath, item)
        else:
            USBDrive = USBDrive + ";" + os.path.join(DefaultUSBPath, item)
if USBDrive is None:
    print ("Error(0) - No USB Drive detected")
    sys.exit()
elif ";" in USBDrive:
    print ("Error(1) - More than one USB Drive detected.")
    sys.exit()
#
# Adding to playlist - Returns directory contents and adds to playlist
#
def addplaylist(track):
    list = None
    if os.path.isdir(os.path.join(USBDrive, track)):
        files = [f for f in os.listdir(os.path.join(USBDrive, track)) if os.path.isfile(os.path.join(USBDrive, track, f))]
        for f in files:
            if list is None:
                list = os.path.join(USBDrive, track, f)
            else:
                list = list + ";" + os.path.join(USBDrive, track, f)
    else:
        print ("Error(2) - Selection is invalid")
    if list is not None:
        if ";" in list:
            list = list.split(";")
    else:
        print ("Error(3) - Selection has no media")
    return list
#
# MediaPlayer function
#
def MusicPlayer(P):
    global Playlist, player
    while len(Playlist) > 0:
        song=Playlist.pop(0)
        print("Song: ")
        print(song)
        player=vlc.MediaPlayer(song)
        player.play()
#
# Define keyboard actions
#
def on_press(key):
    global Action, Playlist, player
    try:
        Sel_char = int(key.char)
    except:
        try:
            Sel_char = str(key.char)
            Sel_char = Sel_char.upper()
        except:
            Sel_char = None
    if Sel_char == "Z":
        return False
    elif Sel_char == "Y":
        print("Skip")
        #player.next()     This line causes 'MediaPlayer' object has no attribute 'next'
        time.sleep(1)
        MusicPlayer(Playlist)
    elif type(Sel_char) == str:
        Action = Sel_char
    elif type(Sel_char) == int:
        Action = Action + str(Sel_char)
        print("Action: " + Action)
        Plist = addplaylist(Action)
        if Plist is not None:
            print("Added to playlist")
            Playlist.append(Plist)
            print(Plist)
            MusicPlayer(Playlist)
    else:
        pass
#
# Read keyboard input
#
with Listener(on_press=on_press) as listener:
    listener.join()
print ("")
print ("Have a nice day!")
print ("")
sys.exit()

Source: Python Questions

LEAVE A COMMENT