VLC Python Bindings: Unable to load another file after EOF
Posted: 24 Feb 2012 18:25
Hi everyone,
I currently try to program a little audioplayer in python, with the use of the libvlc Python bindings.
The problem is, that I cannot load a media file after the current song ended. While the song is playing I can load a new file and play it without problems.
If you need it, here is the code (I hope it is understandable, I am new to Python and libvlc):
(The code is actually meaned to be used with an arduino, which prints a few sensor readings, you can ignore that)
There must be a problem in the eof callback (AudioPlayer.trackEnded())...
I hope you can help me
kind regards
I currently try to program a little audioplayer in python, with the use of the libvlc Python bindings.
The problem is, that I cannot load a media file after the current song ended. While the song is playing I can load a new file and play it without problems.
If you need it, here is the code (I hope it is understandable, I am new to Python and libvlc):
Code: Select all
import os
import string
import time
import vlc
import serial
import io
import struct
arduinoMode = False
class AudioPlayer:
def __init__(self):
self.instance = vlc.Instance()
self.mediaplayer = self.instance.media_player_new()
self.event_manager = self.mediaplayer.event_manager()
self.event_manager.event_attach(vlc.EventType.MediaPlayerEndReached, self.trackEnded)
self.titleNum = 0
self.playlist = []
def playlistFromDir(self, rootDir):
for root, subFolders, files in os.walk(rootDir):
for file in files:
self.playlist.append(os.path.join(root,file))
def loadTrack(self, path):
return self.instance.media_new(path)
print("track loaded")
def playTrack(self, 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 prevTrack(self):
self.setTitle(self.getTitle()-1)
self.playCurrTrack()
def nextTrack(self):
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]))
def getVolume(self):
return self.mediaplayer.audio_get_volume()
def setVolume(self, vol):
if vol < 0:
vol = 0
elif vol > 100:
vol = 100
self.mediaplayer.audio_set_volume(vol)
print("Volume: %i" %(self.mediaplayer.audio_get_volume()))
if arduinoMode == True:
ser = serial.Serial("/dev/ttyACM0", 9600)
connection = False;
while not connection:
input = ser.readline()
print(input)
if input != None:
connection = True
#time.sleep(0.05)
player = AudioPlayer()
player.playlistFromDir("/home/jonas/Musik/test")
player.playCurrTrack()
quit = False
while not quit:
key = raw_input()
if key == "q":
quit = True
if key == "y":
player.prevTrack()
if key == "x":
player.nextTrack()
if key == "-":
player.setVolume( player.getVolume()-5 )
if key == "+":
player.setVolume( player.getVolume()+5 )
if key == "t":
print(player.mediaplayer.get_time())
if arduinoMode == True:
msg = ser.read(1);
print(msg)
player.setVolume(msg)
time.sleep(1)
There must be a problem in the eof callback (AudioPlayer.trackEnded())...
I hope you can help me
kind regards