如何使我的代码随机播放多首歌曲?

时间:2020-08-07 01:52:42

标签: python python-3.x windows python-requests audio-player

对此我有点陌生,需要一点帮助。 我有这段代码,它的基本作用是从您当前所在的目录中播放一首歌曲,我想知道是否可以通过某种方式使其随机播放多首歌曲,而无需重复播放直到列表结束。谢谢

    import random,os,sys

folder=os.listdir(os.getcwd())

file=random.choice(folder)
ext3=['.mp3']
print('First random pick: '+file)

while file[-4:] not in ext3 :
       
       print('Not an MP3 file  : '+file)
       file=random.choice(folder)
else:
       os.startfile(file)
       print('Song name: '+file)



##os.startfile(random.choice(folder))

2 个答案:

答案 0 :(得分:1)

import random, os, sys

folder = os.listdir(os.getcwd())

mp3s = [file for file in folder if file.endswith('.mp3')]
queue = random.shuffle(mp3s)
for file in queue:
    print('Song name:', file)
    os.startfile(file)

答案 1 :(得分:0)

首次运行该程序时,请在目录中创建一系列当前歌曲。然后,从阵列中随机播放一首歌曲。一旦开始播放歌曲,请从阵列中弹出歌曲,以便列表仅包含未播放的歌曲。使用以前的逻辑遍历数组以遍历数组,直到没有歌曲剩下为止。