Skip to content

Instantly share code, notes, and snippets.

@lewis-carson
Last active December 10, 2017 09:34
Show Gist options
  • Save lewis-carson/044c38315d2118b75c76c7ecb76611bd to your computer and use it in GitHub Desktop.
Save lewis-carson/044c38315d2118b75c76c7ecb76611bd to your computer and use it in GitHub Desktop.
A lightweight music player that runs in the console. Use the mode as 's' for shuffle or 'p' for normal play. Add 'r' to the end of the mode to make it repeat. Ctrl + C brings you to the start.
import winsound
import os
import random
from colorama import Fore, Back, Style
def printc(text):
print(Fore.RED + text)
print(Fore.WHITE)
def shuffledir(dir, repeat):
#shuffle
sd = dir[1]
random.shuffle(sd)
#clear
os.system('cls')
#play
for i in sd:
printc(i)
try:
winsound.PlaySound(dir[0]+"\\"+i, winsound.SND_FILENAME)
except KeyboardInterrupt:
return
#check if repeating
if repeat == True:
shuffledir(dir, repeat)
def playdir(dir, repeat):
#clear
os.system('cls')
#play
for i in dir[1]:
printc(i)
try:
winsound.PlaySound(dir[0]+"\\"+i, winsound.SND_FILENAME)
except KeyboardInterrupt:
return
#check if repeating
if repeat == True:
playdir(dir, repeat)
def getdir():
dir = input('Which directory...\n\n')
return [dir, os.listdir(dir)]
def main():
os.system('cls')
m = input('Which mode...\n\n')
if m[0] == 'p':
info = getdir()
repeat = False
if(m[-1] == 'r'):
repeat = True
playdir(info, repeat)
if m[0] == 's':
info = getdir()
repeat = False
if(m[-1] == 'r'):
repeat = True
shuffledir(info, repeat)
while True:
main()
#todo
#better syntax
#comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment