I'm working on a Raspberry Pi 4B and I'm creating a music player to replace the inside of a jukebox. I'm trying to feed music to VLC using key combinations. In this case, selecting 'A1' will go to the USB stick mounted as '/media/usb1' to the 'A1' folder, add any media found to a playlist being played by a media_list_player. This works as designed.
My problem comes in when I'm trying to clear the media list to start over. I need to use the same media_list_player name, but once I create a new media list I can't restart the player.
Here is my snippet of code:
#!/usr/bin/env python3
#
import os, sys, csv, vlc, time, serial, configparser, termios, tty, subprocess
#
# Get Defaults
#
SoundBeforeSong = None
SoundAfterSong = None
TrackDelay = None
PlayHistory = None
USBDrive = "/media/usb1/"
#
# Get keyboard input
#
def getch(): # getchar(), getc(stdin) #PYCHOK flake
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old)
return ch
#
# 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:
if SoundBeforeSong:
list = SoundBeforeSong + ";" + os.path.join(USBDrive, track, f)
else:
list = os.path.join(USBDrive, track, f)
if SoundAfterSong:
list = list + ";" + SoundAfterSong
else:
if SoundBeforeSong:
list = list + ";" + SoundBeforeSong + ";" + os.path.join(USBDrive, track, f)
else:
list = list + ";" + os.path.join(USBDrive, track, f)
if SoundAfterSong:
list = list + ";" + SoundAfterSong
else:
print ("Error(3) - Selection folder not present")
if list is None:
print ("Error(4) - Selection has no media")
else:
if ";" in list:
list = list.split(";")
for song in list:
media_list.add_media(song)
else:
media_list.add_media(list)
print("add media play")
if not media_player.is_playing():
media_player.play()
return
#
# Define keyboard actions
#
def Jukebox():
global Action, Bluetooth
Sel_char = None
while True:
key = getch()
try:
Sel_char = int(key)
except:
try:
Sel_char = key.upper()
except:
Sel_char = None
if Sel_char == "Z":
return False
elif Sel_char == "W":
if not media_player.is_playing():
media_player.play()
if media_player.is_playing():
media_player.stop()
media_list = player.media_list_new() # creating a new media list
media_player.set_media_list(media_list) # setting media list to the media player
elif type(Sel_char) == str:
Action = Sel_char
elif type(Sel_char) == int:
Action = Action + str(Sel_char)
addplaylist(Action)
else:
pass
#
# Setting Up Media Player
#
# creating Instance class object
player = vlc.Instance('--no-xlib --quiet ') # no-xlib for linux and quiet don't complain
media_player = vlc.MediaListPlayer() # creating a media player object
media_list = player.media_list_new() # creating a new media list
media_player.set_media_list(media_list) # setting media list to the media player
new = player.media_player_new() # new media player instance
media_player.set_media_player(new) # setting media player to it
media_events = new.event_manager() # setting event handler
#
# Read keyboard input
#
print("Ready...")
Jukebox()
#
# Program is shutting down
#
sys.exit()
For me, I hit 'A1' and music starts as expected. I hit 'W' and it stops as expected. behind the curtains, I'm also clearing the media list by creating a new blank one. Hitting 'W' again does nothing as I cleared the list, so that makes sense. I hit 'A1' and expect to hear music again, but I don't. I see "add media play", but no sound. How can I restart the media list player? Or probably the more appropriate question is, how do I clear the media list (by creating new or removing media from the list) and allow the media player to be used using the same reference?