我目前正在使用python3和afplay编写音频播出系统。我遇到了一个问题,我不能让它自动转到播放列表中的下一首歌,同时仍允许用户控制。
for i in range(metadata["length"] - start):
i += start
curr = songs[i]
play(directory + curr)
paused_bool = False
while True:
clear()
print("Currently playing track #" + str(i+1) + ", called '" + curr + "', from playlist '" + metadata["name"] + "'.")
command = input("What would you like to do? 'play', 'stop/exit', 'pause', or 'skip/next': ")
if command == "play" or command == "pl":
if not paused_bool:
print("It is already playing.")
else:
play(directory + curr)
elif command == "stop" or command == "st" or command == "quit" or command == "q" or command == "exit":
stop()
return
elif command == "pause" or command == "p" or command == "pa":
if paused_bool:
print("The audio shall remain paused.")
paused_bool = True
stop()
elif command == "move the hell on" or command == "skip" or command == "next" or command == "n" or command == "s" or command == "":
stop()
break
clear()
以上是播放文件夹中每首歌曲的代码。我希望能够使用" next"手动转到下一首歌曲。命令,但也经过一段时间后自动转到下一个,特别是歌曲的长度。
我尝试使用threading
包,但是当我无法从函数中断while循环时遇到问题:
for i in range(metadata["length"] - start):
i += start
curr = songs[i]
play(directory + curr)
paused_bool = False
while True:
clear()
def auto():
time.sleep( length( curr_song ) )
stop() # stop() & break goes to the next song
break
thread = Thread(target = auto, args = [])
thread.start()
print("Currently playing track #" + str(i+1) + ", called '" + curr + "', from playlist '" + metadata["name"] + "'.")
command = input("What would you like to do? 'play', 'stop/exit', 'pause', or 'skip/next': ")
if command == "play" or command == "pl":
if not paused_bool:
print("It is already playing.")
else:
play(directory + curr)
elif command == "stop" or command == "st" or command == "quit" or command == "q" or command == "exit":
stop()
return
elif command == "pause" or command == "p" or command == "pa":
if paused_bool:
print("The audio shall remain paused.")
paused_bool = True
stop()
elif command == "move the hell on" or command == "skip" or command == "next" or command == "n" or command == "s" or command == "":
stop()
break
clear()
我很抱歉这个问题非常令人难以置信,但任何帮助都会受到高度赞赏。
答案 0 :(得分:0)
您可能想要使用scheduler
。
它会稍微改变你的代码的逻辑,但它可能会更有用。
默认情况下,您可以安排播放下一首歌曲的事件(使用scrollTo()
或// Scroll To Query.
submit = () => {
try {
const { text } = this.state // Text.
const target = this[text] // Target.
// Locate Target.
target.measure((framex, framey, width, height, x, y) => {
// Scroll To Target.
this.ScrollView.scrollTo({x, y, animated: true})
})
} catch (error) {
return console.log(error)
}
}
)。您可以获得当前歌曲的运行时间,以便知道何时安排下一首歌曲。
如果" next"如果选中,您将enter
上一个事件并安排另外两个:1)一个新的立即执行,播放下一首歌曲,2)播放以下歌曲。