我想循环播放音乐
I want to play music in a loop
我用 Pythonista 制作音乐播放器。单个mp3文件播放正常,但如果我播放多个mp3文件,它们会同时播放。
有人知道吗?
这是我的代码:
import sound
import time
import glob
fileList = glob.glob("i7/*") #my folder
playerlist = []
for file in fileList:
filename = file
sound.set_honors_silent_switch(False)
sound.set_volume(1)
player = sound.Player(filename)
playerlist.append(player)
for playerMin in playerlist:
playerMin.play() #same time
从docs开始,Player.play()
只是"Starts playing audio."如果你想连续播放这首歌,你必须等到上一首歌结束才能播放一首歌,因为play
在歌曲播放完毕之前不会阻塞主线程。
试试这个:
import sound
import time
### Setup ###
sound.set_honors_silent_switch(False)
sound.set_volume(1)
### You populate this ###
files = [...]
### Play songs in order ###
for filename in files:
player = sound.Player(filename)
player.play()
time.sleep(player.duration) # this is the key part –– makes the loop wait to play the next song until this song is done playing
player.stop()
这个方法超级灵活。例如,如果您想在歌曲之间淡入淡出,您可以在使循环 sleep
比歌曲持续时间短一点的同时提高音量。这变得更复杂,但它基本上是这样完成的。
我用 Pythonista 制作音乐播放器。单个mp3文件播放正常,但如果我播放多个mp3文件,它们会同时播放。
有人知道吗?
这是我的代码:
import sound
import time
import glob
fileList = glob.glob("i7/*") #my folder
playerlist = []
for file in fileList:
filename = file
sound.set_honors_silent_switch(False)
sound.set_volume(1)
player = sound.Player(filename)
playerlist.append(player)
for playerMin in playerlist:
playerMin.play() #same time
从docs开始,Player.play()
只是"Starts playing audio."如果你想连续播放这首歌,你必须等到上一首歌结束才能播放一首歌,因为play
在歌曲播放完毕之前不会阻塞主线程。
试试这个:
import sound
import time
### Setup ###
sound.set_honors_silent_switch(False)
sound.set_volume(1)
### You populate this ###
files = [...]
### Play songs in order ###
for filename in files:
player = sound.Player(filename)
player.play()
time.sleep(player.duration) # this is the key part –– makes the loop wait to play the next song until this song is done playing
player.stop()
这个方法超级灵活。例如,如果您想在歌曲之间淡入淡出,您可以在使循环 sleep
比歌曲持续时间短一点的同时提高音量。这变得更复杂,但它基本上是这样完成的。