Python LibVLC Help Required

This forum is about all development around libVLC.
northwarks
New Cone
New Cone
Posts: 2
Joined: 10 Aug 2016 21:09

Python LibVLC Help Required

Postby northwarks » 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:

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]))
Its called like this:

Code: Select all

def main(): try: print "Ready.." global myplayer myplayer = AudioPlayer() myplayer.add_to_playlist(random_selector()) myplayer.playCurrTrack()
and has a function to generate the random track:

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
and I can add a track (to simulate the jukebox) with GPIO

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")
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") ?

Code: Select all

def playTrack(self, track): print ("Entered playTrack") print track self.mediaplayer.set_media(track) print("PLAYING TRACK") self.mediaplayer.play()
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>

northwarks
New Cone
New Cone
Posts: 2
Joined: 10 Aug 2016 21:09

Re: Python LibVLC Help Required

Postby northwarks » 25 Aug 2016 14:50

Guys - I've given up on VLC, just couldn't get this working and it officially beat me, there's very little code examples I could find out there to give any pointers that specifically discuss refreshing/reloading of playlists in python, Answer could of course be staring me in the face but I just can't see it b :roll: .

I've finally gone down the route of MPD/MPC, taking advantage of the built-in playlist functionality. But thanks for looking !


Return to “Development around libVLC”

Who is online

Users browsing this forum: No registered users and 32 guests