Advertisement
Techpad

MusicPlayer 4

Mar 16th, 2021
1,684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
E 8.43 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import filedialog as fd
  3. import os
  4. import sys
  5. import winsound
  6. import math
  7. import time
  8. from threading import Thread
  9. global sf
  10. try:
  11.     import soundfile as sf
  12. except ModuleNotFoundError:
  13.     os.system('pip install soundfile')
  14.     import soundfile as sf
  15.  
  16. musicfolderpath = 'T:/Storage/Music'
  17. musicfolderall = os.listdir(musicfolderpath)
  18. musicfolder = [file for file in musicfolderall if file.endswith(".wav")]
  19.  
  20. def pause(secs):
  21.     global time
  22.     init_time = time.time()
  23.     while time.time() < init_time+secs: pass
  24.  
  25. def gettime(secs):
  26.     apsecs = round(secs)
  27.     minutes = apsecs / 60
  28.     apminutes = math.floor(minutes)
  29.     minutesecs = apminutes * 60
  30.     exsecs = apsecs - minutesecs
  31.     if len(str(exsecs)) == 1:
  32.         exsecs = "0" + str(exsecs)
  33.     else:
  34.         pass
  35.     time = str(apminutes) + ":" + str(exsecs)
  36.     return time
  37.  
  38. def updatemusicfolder():
  39.     global musicfolder
  40.     try:
  41.         musicfolderall = os.listdir(musicfolderpath)
  42.         musicfolder = [file for file in musicfolderall if file.endswith(".wav")]
  43.     except:
  44.         pass
  45. updatemusicfolder()
  46.  
  47. def changepath():
  48.     global musicfolderpath
  49.     global updatemusicfolder
  50.     global checkmusic
  51.     settingsw.destroy()
  52.     newpath = fd.askdirectory(title='Select Music Folder')
  53.     if not newpath == '':
  54.         musicfolderpath = newpath
  55.         updatemusicfolder()
  56.         checkmusic()
  57.     else:
  58.         pass
  59.  
  60. _root = frame1
  61.  
  62. currentlyplaying = tk.StringVar()
  63. currentlyplaying.set("Now Playing: Nothing currently playing")
  64.  
  65. musicplaying = False
  66.  
  67. def playmusic(file):
  68.     global playmusicthread
  69.     global Thread
  70.     Thread(target=lambda: playmusicthread(file), daemon=True).start()
  71.  
  72. def playmusicthread(file):
  73.     global currentlyplaying
  74.     global musicplaying
  75.     musicplaying = False
  76.     global musicfolderpath
  77.     global winsound
  78.     path = os.path.join(musicfolderpath, file)
  79.     filename = file[:-4]
  80.     if len(filename) > 25:
  81.         cutfilename = filename[:22] + '...'
  82.     else:
  83.         cutfilename = filename
  84.     winsound.PlaySound(path, winsound.SND_ASYNC)
  85.     soundf = sf.SoundFile(path)
  86.     seconds = len(soundf) / soundf.samplerate
  87.     apsecs = round(seconds)
  88.     fulltime = gettime(apsecs)
  89.     playedsecs = 0
  90.     currentlyplaying.set("Now Playing: " + cutfilename + "  (" + gettime(playedsecs) + " / " + fulltime + ")")
  91.     musicplaying = True
  92.     while musicplaying == True and not playedsecs == apsecs:
  93.         pause(1)
  94.         playedsecs += 1
  95.         currentlyplaying.set("Now Playing: " + cutfilename + "  (" + gettime(playedsecs) + " / " + fulltime + ")")
  96.     musicplaying = False
  97.  
  98. playingall = False
  99.  
  100. def playallthread():
  101.     global musicfolder
  102.     global musicplaying
  103.     global playingall
  104.     global pause
  105.     playingall = True
  106.     for element in musicfolder:
  107.         pause(1)
  108.         if playingall == True:
  109.             playmusic(element)
  110.             while True:
  111.                 if musicplaying == True:
  112.                     while musicplaying == True:
  113.                         pass
  114.                     break
  115.                 else:
  116.                     pass
  117.         else:
  118.             break
  119.  
  120. def playall():
  121.     global playallthread
  122.     global thread
  123.     Thread(target=playallthread, daemon=True).start()
  124.  
  125. def stopmusic():
  126.     global musicplaying
  127.     global playingall
  128.     global winsound
  129.     playingall = False
  130.     musicplaying = False
  131.     winsound.PlaySound(None, winsound.SND_PURGE)
  132.  
  133. container = tk.Frame(_root, bd=0, bg='white')
  134. canvas = tk.Canvas(container, bg='white', bd=0, highlightthickness=0)
  135. scrollbar = tk.Scrollbar(container, orient="vertical", command=canvas.yview, troughcolor='white', width=12)
  136.  
  137. view = 'compact' # other options are normal and large
  138.  
  139. def changeview():
  140.     global view
  141.     if view == 'compact':
  142.         view = 'normal'
  143.     elif view == 'normal':
  144.         view = 'large'
  145.     elif view == 'large':
  146.         view = 'compact'
  147.     checkmusic()
  148.  
  149. def settings():
  150.     global view
  151.     global changepath
  152.     global fd
  153.     global changeview
  154.     global checkmusic
  155.     global settingsw
  156.     global tk
  157.     settingsw = tk.Toplevel(root)
  158.     settingsw.minsize(400, 300)
  159.     settingsw.resizable(False, False)
  160.     settingsw.title('Settings')
  161.     settingsw.configure(bg='white')
  162.     settingslabel = tk.Label(settingsw, text='Settings', bg='white', font='arial 20')
  163.     settingslabel.pack(side='top', anchor='nw', padx=10, pady=10)
  164.     changepathbutton = tk.Button(settingsw, text='Change music folder', command=changepath, bd=1, relief='solid', bg='white')
  165.     changepathbutton.pack(side='top', anchor='nw', padx=10, pady=10)
  166.     currentview = tk.StringVar()
  167.     if view == 'compact':
  168.         currentview.set('View: Compact')
  169.     elif view == 'normal':
  170.         currentview.set('View: Normal')
  171.     elif view == 'large':
  172.         currentview.set('View: Large')
  173.     def cycleview():
  174.         changeview()
  175.         if view == 'compact':
  176.             currentview.set('View: Compact')
  177.         elif view == 'normal':
  178.             currentview.set('View: Normal')
  179.         elif view == 'large':
  180.             currentview.set('View: Large')
  181.     cycleviewbutton = tk.Button(settingsw, textvar=currentview, command=cycleview, bd=1, relief='solid', bg='white')
  182.     cycleviewbutton.pack(side='top', anchor='nw', padx=10, pady=10)
  183.  
  184. def checkmusic():
  185.     global musicframe
  186.     global musicfolder
  187.     global canvas
  188.     global scrollbar
  189.     global musicfolderpath
  190.     global view
  191.     global gettime
  192.     global math
  193.     global playmusic
  194.     global tk
  195.     global os
  196.     try:
  197.         musicframe.destroy()
  198.     except:
  199.         pass
  200.     musicframe = tk.Frame(canvas, bd=0, bg='white', highlightcolor='white', highlightthickness=0)
  201.     musicframe.bind(
  202.         "<Configure>",
  203.         lambda e: canvas.configure(
  204.             scrollregion=canvas.bbox("all")
  205.         )
  206.     )
  207.     canvas.create_window((0, 0), window=musicframe, anchor="nw")
  208.     canvas.configure(yscrollcommand=scrollbar.set)
  209.     if not musicfolder == []:
  210.         for element in musicfolder:
  211.             path = os.path.join(musicfolderpath, element)
  212.             soundf = sf.SoundFile(path)
  213.             seconds = len(soundf) / soundf.samplerate
  214.             if view == 'compact':
  215.                 songbutton = tk.Button(musicframe, bd=0, text="\u25b6 " + element[:-4] + "  (" + gettime(seconds) + ")", wraplength=585, justify='left', bg='white', anchor='w', padx=9, command=lambda element=element: playmusic(element))
  216.             elif view == 'normal':
  217.                 songbutton = tk.Button(musicframe, bd=0, text="\u25b6 " + element[:-4] + "  (" + gettime(seconds) + ")", wraplength=585, justify='left', bg='white', anchor='w', padx=9, pady=3, command=lambda element=element: playmusic(element))
  218.             elif view == 'large':
  219.                 songbutton = tk.Button(musicframe, bd=0, text="\u25b6 " + element[:-4] + "  (" + gettime(seconds) + ")", wraplength=585, justify='left', bg='white', anchor='w', padx=9, pady=6, command=lambda element=element: playmusic(element))
  220.             songbutton.pack(anchor='w', expand=True, fill='x')
  221.     else:
  222.         nomusiclabel = tk.Button(musicframe, text="Hmmm. It seems like there isn't any music in this folder. Click here to change your music folder (Keep in mind, we only support wav formats.)", wraplength=585, bg='white', command=changepath, bd=0)
  223.         nomusiclabel.pack(anchor='center', expand=True, fill='x')
  224.     scrollbar.update()
  225.  
  226. container.pack(expand=True, fill='both')
  227. canvas.place(x=0, y=0, relwidth=1.0, relheight=1.0)
  228. scrollbar.pack(side="right", fill="y")
  229. stopbutton = tk.Button(container, text='\u25a0', command=stopmusic, bd=1, relief='solid', bg='white', padx=5)
  230. stopbutton.pack(side='right', anchor='se', padx=5, pady=5)
  231. playallbutton = tk.Button(container, text='\u25b6 All', command=playall, bd=1, relief='solid', bg='white', padx=5)
  232. playallbutton.pack(side='right', anchor='se', padx=5, pady=5)
  233. refreshbutton = tk.Button(container, text='\u27f3', command=checkmusic, bd=1, relief='solid', bg='white', padx=5)
  234. refreshbutton.pack(side='right', anchor='se', padx=5, pady=5)
  235. settingsbutton = tk.Button(container, text='Settings', command=settings, bd=1, relief='solid', bg='white')
  236. settingsbutton.pack(side='right', anchor='se', padx=5, pady=5)
  237. currentlyplayinglabel = tk.Label(container, textvar=currentlyplaying, bg='white', justify='right', bd=1, relief='solid', padx=4, pady=3)
  238. currentlyplayinglabel.pack(side='bottom', anchor='se', padx=5, pady=5)
  239. checkmusic()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement