简体   繁体   中英

How do I make my code play more than one song randomly?

I'm a bit new to this and I would need a little help with this; I have this code that what it basically does is play you a song from the current directory you are in, I would like to know if you could somehow make it play more than one song randomly without repeating itself until the list is over. Thank you

    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))
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)

When you first run the program, create an array of current songs in the directory. Then, randomly play a song from the array. Once you begin playing a song, pop the song from the array so that the list only contains unplayed songs. Iterate over the array using the former logic to work through the array until no songs remain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM