Python VLC - 下一首曲目
Python VLC - Next track
我正在 Python 到 运行 在 Raspberry Pi 上编写一个程序来控制我的 Wurlitzer 自动点唱机。当前程序接受“记录选择”代码(A1、B1、C4 等),将这些代码添加到播放列表,然后执行该列表。我的问题是,一旦歌曲开始播放,我希望能够按下一个按钮(当前代码中的“Y”)来跳过当前播放的歌曲。我无法让它工作。
如果我使用“player.next()”,我会得到一个错误:'MediaPlayer' object has no attribute 'next'.
我试图停止播放器并重新启动它(认为它会播放播放列表中的下一首歌曲。这甚至没有停止播放器。
如果可以避免,我不想使用子流程。我想在 Python 内想出一种方法来跳过。如何做到这一点?
import os, sys, csv, vlc, time, threading
from pynput.keyboard import Key, Listener
DefaultUSBPath="/media/pi"
PlayHistory="PlayHistory.csv"
#
# Declare variables
#
USBDrive = None
Action = None
Playlist = []
SelectionCount = []
Sel_char = None
#
# Find the USB Drive
#
for item in os.listdir(DefaultUSBPath):
if os.path.isdir(os.path.join(DefaultUSBPath, item)):
if USBDrive is None:
USBDrive = os.path.join(DefaultUSBPath, item)
else:
USBDrive = USBDrive + ";" + os.path.join(DefaultUSBPath, item)
if USBDrive is None:
print ("Error(0) - No USB Drive detected")
sys.exit()
elif ";" in USBDrive:
print ("Error(1) - More than one USB Drive detected.")
sys.exit()
#
# 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:
list = os.path.join(USBDrive, track, f)
else:
list = list + ";" + os.path.join(USBDrive, track, f)
else:
print ("Error(2) - Selection is invalid")
if list is not None:
if ";" in list:
list = list.split(";")
else:
print ("Error(3) - Selection has no media")
return list
#
# MediaPlayer function
#
def MusicPlayer(P):
global Playlist, player
while len(Playlist) > 0:
song=Playlist.pop(0)
print("Song: ")
print(song)
player=vlc.MediaPlayer(song)
player.play()
#
# Define keyboard actions
#
def on_press(key):
global Action, Playlist, player
try:
Sel_char = int(key.char)
except:
try:
Sel_char = str(key.char)
Sel_char = Sel_char.upper()
except:
Sel_char = None
if Sel_char == "Z":
return False
elif Sel_char == "Y":
print("Skip")
#player.next() This line causes 'MediaPlayer' object has no attribute 'next'
time.sleep(1)
MusicPlayer(Playlist)
elif type(Sel_char) == str:
Action = Sel_char
elif type(Sel_char) == int:
Action = Action + str(Sel_char)
print("Action: " + Action)
Plist = addplaylist(Action)
if Plist is not None:
print("Added to playlist")
Playlist.append(Plist)
print(Plist)
MusicPlayer(Playlist)
else:
pass
#
# Read keyboard input
#
with Listener(on_press=on_press) as listener:
listener.join()
print ("")
print ("Have a nice day!")
print ("")
sys.exit()
根据您的编码方式,我希望您必须停止它,从列表中删除当前媒体,然后重新启动它。
但是 运行 一个 media_list_player
可能会更好,请参阅下面的基本版本。请注意,我在 Linux 上,不得不破解一些代码来获取键输入,而不是使用特定的库或花时间在上面,但它应该足以让你使用。
Edit
抱歉,使用 media_list_player
时有一个更简单的方法,但如果您想要更好的控制,您应该使用 media_list_player
来控制列表的索引,或者要完全控制,请使用 a media_player_new()
但这超出了这个问题的范围。 (我会把原来的答案留在这段代码下面,因为它可能会有用)
import vlc
import time
## pinched from vlc for keyboard input
import termios
import tty
import sys
mymedia = ["vp.mp3","vp1.mp3","happy.mp3","V1.mp4"]
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
## end pinched code
def jukebox():
player.play_item_at_index(0)
while True:
time.sleep(0.25)
k = getch()
if k == "n": #Next
player.next()
if k == "p": #Previous
player.previous()
if k == " ": #Pause
player.pause()
if k == "q": #Quit
player.stop()
return True
player.next()
vlc_instance = vlc.Instance('--no-xlib --quiet ') # no-xlib for linux and quiet don't complain
player = vlc_instance.media_list_player_new()
Media = vlc_instance.media_list_new(mymedia)
player.set_media_list(Media)
print("Welcome to Jukebox")
print("Options - space = Play/Pause, n = Next, p = Previous, q = Quit")
print("Media",mymedia)
jukebox()
Original code
import vlc
import time
## pinched from vlc for keyboard input
import termios
import tty
import sys
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
## end pinched code
vlc_instance = vlc.Instance()
player = vlc_instance.media_list_player_new()
mymedia = ["vp.mp3","vp1.mp3","happy.mp3"]
Media = vlc_instance.media_list_new(mymedia)
player.set_media_list(Media)
for index, name in enumerate(mymedia):
print("Playing:",name)
player.play_item_at_index(index)
time.sleep(1)
while player.get_state() != 6:
time.sleep(1)
k = getch()
if k == "y":
player.stop()
break
我正在 Python 到 运行 在 Raspberry Pi 上编写一个程序来控制我的 Wurlitzer 自动点唱机。当前程序接受“记录选择”代码(A1、B1、C4 等),将这些代码添加到播放列表,然后执行该列表。我的问题是,一旦歌曲开始播放,我希望能够按下一个按钮(当前代码中的“Y”)来跳过当前播放的歌曲。我无法让它工作。
如果我使用“player.next()”,我会得到一个错误:'MediaPlayer' object has no attribute 'next'.
我试图停止播放器并重新启动它(认为它会播放播放列表中的下一首歌曲。这甚至没有停止播放器。
如果可以避免,我不想使用子流程。我想在 Python 内想出一种方法来跳过。如何做到这一点?
import os, sys, csv, vlc, time, threading
from pynput.keyboard import Key, Listener
DefaultUSBPath="/media/pi"
PlayHistory="PlayHistory.csv"
#
# Declare variables
#
USBDrive = None
Action = None
Playlist = []
SelectionCount = []
Sel_char = None
#
# Find the USB Drive
#
for item in os.listdir(DefaultUSBPath):
if os.path.isdir(os.path.join(DefaultUSBPath, item)):
if USBDrive is None:
USBDrive = os.path.join(DefaultUSBPath, item)
else:
USBDrive = USBDrive + ";" + os.path.join(DefaultUSBPath, item)
if USBDrive is None:
print ("Error(0) - No USB Drive detected")
sys.exit()
elif ";" in USBDrive:
print ("Error(1) - More than one USB Drive detected.")
sys.exit()
#
# 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:
list = os.path.join(USBDrive, track, f)
else:
list = list + ";" + os.path.join(USBDrive, track, f)
else:
print ("Error(2) - Selection is invalid")
if list is not None:
if ";" in list:
list = list.split(";")
else:
print ("Error(3) - Selection has no media")
return list
#
# MediaPlayer function
#
def MusicPlayer(P):
global Playlist, player
while len(Playlist) > 0:
song=Playlist.pop(0)
print("Song: ")
print(song)
player=vlc.MediaPlayer(song)
player.play()
#
# Define keyboard actions
#
def on_press(key):
global Action, Playlist, player
try:
Sel_char = int(key.char)
except:
try:
Sel_char = str(key.char)
Sel_char = Sel_char.upper()
except:
Sel_char = None
if Sel_char == "Z":
return False
elif Sel_char == "Y":
print("Skip")
#player.next() This line causes 'MediaPlayer' object has no attribute 'next'
time.sleep(1)
MusicPlayer(Playlist)
elif type(Sel_char) == str:
Action = Sel_char
elif type(Sel_char) == int:
Action = Action + str(Sel_char)
print("Action: " + Action)
Plist = addplaylist(Action)
if Plist is not None:
print("Added to playlist")
Playlist.append(Plist)
print(Plist)
MusicPlayer(Playlist)
else:
pass
#
# Read keyboard input
#
with Listener(on_press=on_press) as listener:
listener.join()
print ("")
print ("Have a nice day!")
print ("")
sys.exit()
根据您的编码方式,我希望您必须停止它,从列表中删除当前媒体,然后重新启动它。
但是 运行 一个 media_list_player
可能会更好,请参阅下面的基本版本。请注意,我在 Linux 上,不得不破解一些代码来获取键输入,而不是使用特定的库或花时间在上面,但它应该足以让你使用。
Edit
抱歉,使用 media_list_player
时有一个更简单的方法,但如果您想要更好的控制,您应该使用 media_list_player
来控制列表的索引,或者要完全控制,请使用 a media_player_new()
但这超出了这个问题的范围。 (我会把原来的答案留在这段代码下面,因为它可能会有用)
import vlc
import time
## pinched from vlc for keyboard input
import termios
import tty
import sys
mymedia = ["vp.mp3","vp1.mp3","happy.mp3","V1.mp4"]
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
## end pinched code
def jukebox():
player.play_item_at_index(0)
while True:
time.sleep(0.25)
k = getch()
if k == "n": #Next
player.next()
if k == "p": #Previous
player.previous()
if k == " ": #Pause
player.pause()
if k == "q": #Quit
player.stop()
return True
player.next()
vlc_instance = vlc.Instance('--no-xlib --quiet ') # no-xlib for linux and quiet don't complain
player = vlc_instance.media_list_player_new()
Media = vlc_instance.media_list_new(mymedia)
player.set_media_list(Media)
print("Welcome to Jukebox")
print("Options - space = Play/Pause, n = Next, p = Previous, q = Quit")
print("Media",mymedia)
jukebox()
Original code
import vlc
import time
## pinched from vlc for keyboard input
import termios
import tty
import sys
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
## end pinched code
vlc_instance = vlc.Instance()
player = vlc_instance.media_list_player_new()
mymedia = ["vp.mp3","vp1.mp3","happy.mp3"]
Media = vlc_instance.media_list_new(mymedia)
player.set_media_list(Media)
for index, name in enumerate(mymedia):
print("Playing:",name)
player.play_item_at_index(index)
time.sleep(1)
while player.get_state() != 6:
time.sleep(1)
k = getch()
if k == "y":
player.stop()
break