Python libvlc动态播放列表

时间:2016-08-09 10:58:33

标签: python libvlc

我试图将20世纪50年代的自动点唱机转换为在我的洞穴中播放mp3曲目,所以我放弃一枚硬币并选择2首曲目并将它们添加到我的播放列表中,我的Raspberry Pi开始播放轨道。我选择了更多的曲目,并且他们还会加入惠斯特。

我已经掌握了基本代码,但是在播放列表结束后3周后我仍然无法通过libvlc播放曲目,我知道这与加载medialist有关,但我可以&# 39;解决它。

我有一个GPIO输入,可以为播放列表添加新曲目,它似乎是添加它但它们从不播放。

我尝试了很多方法,本周玩了一个我发现让它工作的课程 - 都失败了,那里没有很多示例代码可以看到动态修改播放列表,他们所有这些似乎都适用于那些在用户选择随机曲目的自动点唱机上无法工作的目录。

我所能追踪的是能够在播放时将曲目添加到播放列表中,并且这些曲目会在播放时添加到播放列表中。

class AudioPlayer:

def __init__(self):
    self.instance = vlc.Instance()
    self.mediaplayer = self.instance.media_player_new()
    self.mediaplayer.audio_set_volume(25)
    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.changed_track)
    self.titleNum = 0
    self.playlist = []
    print ("Player Created")

def add_to_playlist(self, newtrack):
    self.playlist.append(newtrack)

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 changed_track(self, event):
    print("Track Changed")

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 main():
try:
  print "Ready.."
  global myplayer
  myplayer = AudioPlayer()
  myplayer.add_to_playlist(random_selector())
  myplayer.playCurrTrack()


  while True:
   time.sleep(1)
except KeyboardInterrupt:
    print "Shutdown requested...exiting"
except Exception:
    sys.exit(0)

if __name__ == "__main__":
    main()

我有一个模仿Jukebox的小方法叫做随机选择器:

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

通过GPIO调用此方法来模拟自动存储塔选择:

def new_track_callback(channel):
  # call new track when button pressed
  global myplayer
  myplayer.add_to_playlist(random_selector())
  print ("New Track Added")

编辑包含输出,基本上我在playTrack中添加了另一个打印命令,以确保它实际上被点击OK,还打印轨道以确保它有一个值:

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>

0 个答案:

没有答案