Python LibVLC Help Required
Posted: 10 Aug 2016 21:31
Hi - So I'm working to convert a 1950s jukebox to play mp3 files using a Raspberry Pi, I'm 'OK' with Python but LibVLC has me really stumped, I've tried for literally 3 weeks to try and get this working in various forms with no success. So I have a bit of code that picks a random track to simulate a jukebox and puts that into a playlist, the problem I have is with a jukebox you drop a coin in and you select another track and add it to the currently playlist whilst its playing to the playlist grows. What I can't get my code to do in what ever form I have tried is to play the next track in the playlist once its been modified. I'm looking for some help in getting this working.
My latest try is with this class I found:
Its called like this:
and has a function to generate the random track:
and I can add a track (to simulate the jukebox) with GPIO
The code runs, plays the first track, the MediaPlayerEndReached, self.trackEnded is called but fails to play the track and just stops at the point below, so I assume its something to do with setting the media as I get the output from print track but not print("PLAYING TRACK") ?
Edited to add the actual output for completeness, so shows it entering the playTrack function and prints the track <vlc.Media object at 0x7649ef50>:
Track ended
Get Next Track
track 2 of 2: /mnt/usb/C4
trying to load new track...
trying to play track...
Entered playTrack
<vlc.Media object at 0x7649ef50>
My latest try is with this class I found:
Code: Select all
class AudioPlayer:
def __init__(self):
self.instance = vlc.Instance()
self.mediaplayer = self.instance.media_player_new()
self.mediaplayer.audio_set_volume(15)
self.event_manager = self.mediaplayer.event_manager()
self.event_manager.event_attach(vlc.EventType.MediaPlayerEndReached, self.trackEnded)
self.event_manager.event_attach(vlc.EventType.MediaPlayerMediaChanged, self.mediaChanged)
self.titleNum = 0
self.playlist = []
print ("Player Created")
def add_to_playlist(self, newtrack):
self.playlist.append(newtrack)
print ("Playlist Updated")
def loadTrack(self, path):
return self.instance.media_new(path)
print("track loaded")
def playTrack(self, track):
print ("Entered playTrack")
print track
self.mediaplayer.set_media(track)
print("PLAYING TRACK")
self.mediaplayer.play()
def playCurrTrack(self):
print("trying to load new track...")
track = self.loadTrack(self.playlist[self.titleNum])
print("trying to play track...")
self.playTrack(track)
def trackEnded(self, event):
print("Track ended")
self.nextTrack()
def mediaChanged(self, event):
print("Media Changed")
def prevTrack(self):
self.setTitle(self.getTitle()-1)
self.playCurrTrack()
def nextTrack(self):
print ("Get Next Track")
self.setTitle(self.getTitle()+1)
self.playCurrTrack()
def getTitle(self):
return self.titleNum
def setTitle(self, number):
self.titleNum = number
if self.titleNum < 0:
self.titleNum = len(self.playlist) + self.titleNum
elif self.titleNum >= len(self.playlist):
self.titleNum = len(self.playlist) - self.titleNum
print("track %i of %i: %s" % (self.titleNum+1, len(self.playlist), self.playlist[self.titleNum]))
Code: Select all
def main():
try:
print "Ready.."
global myplayer
myplayer = AudioPlayer()
myplayer.add_to_playlist(random_selector())
myplayer.playCurrTrack()
Code: Select all
def random_selector():
#removed R for Radio & V for playlists
string.letters="ABCDEFGHJKLMNPQST"
string.numbers="12345678"
LET = random.choice(string.letters)
NUM = random.choice(string.numbers)
track_choice = m_path+LET+NUM
return track_choice
Code: Select all
def new_track_callback(channel):
# call new track when button pressed
global myplayer
myplayer.add_to_playlist(random_selector())
print ("New Track Added")
Code: Select all
def playTrack(self, track):
print ("Entered playTrack")
print track
self.mediaplayer.set_media(track)
print("PLAYING TRACK")
self.mediaplayer.play()
Track ended
Get Next Track
track 2 of 2: /mnt/usb/C4
trying to load new track...
trying to play track...
Entered playTrack
<vlc.Media object at 0x7649ef50>