-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.py
592 lines (437 loc) · 17.2 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
## Importing all the neccesary modules.
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox as mb
import os
from PIL import ImageTk, Image
from pygame import mixer
from tkinter import ttk
from ttkthemes import themed_tk as tk
from mutagen.mp3 import MP3
from mutagen.id3 import ID3
import threading
import time
import random
import _thread
mixer.init()
playing = False
paused = False
mute = False
cur_playing = ''
con_style = 'rep_one'
to_break = False
current_time = 0
## Main Class
class Main_class():
songs = []
play_thread = None
def about(self):
mb.showinfo('About','This is an exclusive distribution of DeePlayer.\n Creator of this apllication is Hardeep Singh.\nThis was completed on 23/6/2019.\n Thanks For Using The Application.')
def start_count(self,t):
# mixer.music.get_busy(): - Returns FALSE when we press the stop button (music stop playing)
# Continue - Ignores all of the statements below it. We check if music is paused or not.
global current_time
while current_time <= t and mixer.music.get_busy():
global paused
global dur_start
global progress_bar
global total_length
global con_style
global to_break
if paused:
continue
elif to_break:
break
else:
mins, secs = divmod(current_time, 60)
mins = round(mins)
secs = round(secs)
timeformat = '{:02d}:{:02d}'.format(mins, secs)
dur_start['text'] = timeformat
time.sleep(1)
current_time += 1
progress_bar['value'] = current_time
progress_bar.update()
if to_break:
to_break = False
current_time=0
return None
else:
try:
self.con_func(con_style)
except:
pass
def show_details(self,play_song):
global dur_end
global progress_bar
global total_length
# global th
file_data = os.path.splitext(play_song)
if file_data[1] == '.mp3':
audio = MP3(play_song)
total_length = audio.info.length
# try:
with open('temp.jpg', 'wb') as img:
a = ID3(play_song)
img.write(a.getall('APIC')[0].data)
image = self.makeAlbumArtImage('temp.jpg')
self.album_art_label.configure(image=image)
self.album_art_label.image = image
# except:
# pass
else:
a = mixer.Sound(play_song)
total_length = a.get_length()
progress_bar['maximum'] = total_length
# div - total_length/60, mod - total_length % 60
mins, secs = divmod(total_length, 60)
mins = round(mins)
secs = round(secs)
timeformat = '{:02d}:{:02d}'.format(mins, secs)
dur_end['text'] = timeformat
self.play_thread = _thread.start_new_thread(self.start_count,(total_length,))
#t1 = threading.Thread(target=self.start_count, args=(total_length,))
#t1.start()
## Add songs to the playlist.
def set_playlist(self):
music_ex = ['mp3','wav','mpeg','m4a','wma','ogg']
dir_ = filedialog.askdirectory(initialdir='D:\\',title='Select Directory')
os.chdir(dir_)
status_bar['text'] = 'Playlist Updated.'
dir_files = os.listdir(dir_)
self.songs = []
for file in dir_files:
exten = file.split('.')[-1]
for ex in music_ex:
if exten == ex:
play_list.insert(END,file)
self.songs.append(file)
def con_func(self,con):
global cur_playing
global current_time
current_time=0
if con == 'rand':
try:
in_ = random.randint(0,len(self.songs))
next_play = self.songs[in_]
self.play_next(next_play)
except:
self.play_music()
elif con == 'rep_all':
try:
in_ = self.songs.index(cur_playing)
next_play = self.songs[in_+1]
self.play_next(next_play)
except:
self.play_music()
else:
self.play_next(cur_playing)
def play_next(self,song):
global playing
global cur_playing
global file
file = song
cur_playing = file
mixer.music.load(file)
mixer.music.play()
status_bar['text'] = 'Playing - '+file
play_button['image'] = pause_img
playing = True
self.show_details(file)
def playSongInitial(self, *args):
self.stop()
self.play_music()
def play_music(self):
global playing
global cur_playing
try:
if playing == False:
global file
file = play_list.get(ACTIVE)
cur_playing = file
mixer.music.load(file)
mixer.music.play()
status_bar['text'] = 'Playing - '+file
play_button['image'] = pause_img
playing = True
self.show_details(file)
else:
global paused
if paused == True:
mixer.music.unpause()
paused = False
status_bar['text'] = 'Playing - '+file
play_button['image'] = pause_img
else:
mixer.music.pause()
paused = True
play_button['image'] = play_img
status_bar['text'] = 'Music Paused'
except:
mb.showerror('error','No file found to play.')
def stop(self):
mixer.music.stop()
global playing
global paused
global dur_start
global progress_bar
global cur_playing
global current_time
global to_break
to_break = True
current_time=0
cur_playing = ''
playing = False
paused = False
dur_start['text'] = '--:--'
dur_end['text'] = '--:--'
progress_bar['value'] = 0.0
progress_bar.update()
self.album_art_label.configure(image=None)
self.album_art_label.image = None
play_button['image'] = play_img
status_bar['text'] = 'Music Stopped'
to_break = False
return None
def next_prev(self,num):
global file
global playing
global to_break
global dur_start
to_break = True
dur_start['text'] = '00:00'
try:
if num == 1:
index = self.songs.index(file) - 1
file = self.songs[index]
mixer.music.load(file)
mixer.music.play()
status_bar['text'] = 'Playing - '+file
play_button['image'] = pause_img
playing = True
self.show_details(file)
else:
index = self.songs.index(file) + 1
file = self.songs[index]
mixer.music.load(file)
mixer.music.play()
status_bar['text'] = 'Playing - '+file
play_button['image'] = pause_img
playing = True
self.show_details(file)
except IndexError:
self.play_music()
except ValueError:
global paused
playing = False
paused = False
self.play_music()
def open_file(self):
dir_ = filedialog.askopenfilename(initialdir='D:/',title='Select File')
cng_dir = dir_.split('/')[0:-1]
cng_dir = ''.join(cng_dir)
os.chdir(cng_dir)
self.songs.append(dir_)
filename = os.path.basename(dir_)
play_list.insert(END,filename)
global playing
playing = False
def set_con(self,num):
global con_style
if num == 1:
con_style = 'rand'
elif num == 2:
con_style = 'rep_all'
else:
con_style = 'rep_one'
def speaker_func(self):
global mute
global status_bar
if mute == False:
speaker['image'] = mute_img
mixer.music.set_volume(0.0)
mute = True
else:
speaker['image'] = speaker_img
num = scale.get()
mixer.music.set_volume(float(num) /100)
mute = False
def set_vol(self,num):
global mute
global status_bar
if num == float(0):
speaker['image'] = mute_img
mixer.music.set_volume(0.0)
mute = True
elif mute == True:
speaker['image'] = speaker_img
num = scale.get()
mixer.music.set_volume(float(num) /100)
mute = False
else:
volume = float(num) / 100
mixer.music.set_volume(volume)
def exit(self):
self.stop()
win.destroy()
sys.exit()
def coming_soon(self):
mb.showinfo('Coming Soon','The Feature You Clicked Will Be Coming Soon.\n Please Wait For An Update. Stay Tuned')
def makeAlbumArtImage(self, image_path):
image = Image.open(image_path)
image = image.resize((350, 350), Image.ANTIALIAS)
return ImageTk.PhotoImage(image)
## Constructer Method - Main method For GUI.
def __init__(self):
## Making Tkinter Window.
global win
win = Tk()
win.geometry('800x520')
win.resizable(0,0)
win.title('DeePlayer')
win.wm_attributes('-alpha',0.95)
win.iconbitmap('icon.ico')
## Menu bar - all the menu_cascades and menu_commands.
main_menu = Menu(win,tearoff=0)
win.configure(menu=main_menu)
file = Menu(main_menu,tearoff=0)
main_menu.add_cascade(label='Media',menu=file)
file.add_command(label='Open',command=self.open_file)
file.add_command(label='Open Folder',command=self.set_playlist)
file.add_command(label='Save Playlist',command=self.coming_soon)
file.add_command(label='Open Muliple Files',command=self.coming_soon)
file.add_command(label='Open Disk',command=self.coming_soon)
file.add_command(label='Open Network Stream',command=self.coming_soon)
file.add_separator()
file.add_command(label='Open Recent Media',command=self.coming_soon)
file.add_command(label='Add Inteface',command=self.coming_soon)
file.add_command(label='Fullscreen',command=self.coming_soon)
file.add_separator()
file.add_command(label='Exit',command=self.exit)
about = Menu(main_menu,tearoff=0)
main_menu.add_cascade(label='About',menu=about)
about.add_command(label='About Us',command=self.about)
#Album Art Part
# album_art_photo = self.makeAlbumArtImage('temp.jpg')
self.album_art_label = Label(win)
self.album_art_label.place(x=85, y=20)
#Playlist Frame
Label(win,text='', bg='White',height=19,width=35,relief_='ridge').place(x=543,y=0)
Button(win, text='Add a Folder.',bd=2,font=('arialblack',13),width=25,command=self.set_playlist).place(x=552,y=10)
global play_list
play_list = Listbox(win,height=21,width=41)
play_list.place(x=544,y=50)
play_list.bind('<Double-Button>', self.playSongInitial)
## Bottom Control Center
Label(win, text='',height=5,relief_='groove',width=200).place(x=0,y=395)
global play_img
play_img = PhotoImage(file='resources/play.png')
def on_enter_play(event):
play_des.place(x=25,y=460)
def on_leave_play(event):
play_des.place(x=1000,y=1000)
global play_button
play_button = Button(win, image=play_img,bd=0,command=self.play_music)
play_button.place(x=10,y=440)
play_button.bind('<Enter>',on_enter_play)
play_button.bind('<Leave>',on_leave_play)
def on_enter_prev(event):
prev_des.place(x=45,y=460)
def on_leave_prev(event):
prev_des.place(x=1000,y=1000)
prev_img = PhotoImage(file='resources/prev.png')
prev_button = Button(win, image=prev_img,bd=0,command=lambda:self.next_prev(1))
prev_button.place(x=50,y=433)
prev_button.bind('<Enter>',on_enter_prev)
prev_button.bind('<Leave>',on_leave_prev)
def on_enter_stop(event):
stop_des.place(x=70,y=460)
def on_leave_stop(event):
stop_des.place(x=1000,y=1000)
stop_img = PhotoImage(file='resources/stop.png')
stop_button = Button(win,image=stop_img,bd=0,command=self.stop)
stop_button.place(x=85,y=438)
stop_button.bind('<Enter>',on_enter_stop)
stop_button.bind('<Leave>',on_leave_stop)
def on_enter_next(event):
next_des.place(x=100,y=460)
def on_leave_next(event):
next_des.place(x=1000,y=1000)
next_img = PhotoImage(file='resources/next.png')
next_button = Button(win, image=next_img,bd=0,command=lambda:self.next_prev(2))
next_button.place(x=113,y=433)
next_button.bind('<Enter>',on_enter_next)
next_button.bind('<Leave>',on_leave_next)
global pause_img
pause_img = PhotoImage(file='resources/pause.png')
global speaker_img
speaker_img = PhotoImage(file='resources/vol.png')
global mute_img
mute_img = PhotoImage(file='resources/mute.png')
def on_enter_vol(event):
vol_des.place(x=560,y=450)
def on_leave_vol(event):
vol_des.place(x=1000,y=1000)
global speaker
speaker = Button(win,image=speaker_img,bd=0,command=self.speaker_func)
speaker.place(x=650,y=442)
speaker.bind('<Enter>',on_enter_vol)
speaker.bind('<Leave>',on_leave_vol)
def on_enter_shuffle(event):
shuffle_des.place(x=180,y=460)
def on_leave_shuffle(event):
shuffle_des.place(x=1000,y=1000)
shuffle_img = PhotoImage(file='resources/shuffle.png')
shuffle_button = Button(win, image=shuffle_img,bd=0,command=lambda:self.set_con(1))
shuffle_button.place(x=170,y=440)
shuffle_button.bind('<Enter>',on_enter_shuffle)
shuffle_button.bind('<Leave>',on_leave_shuffle)
def on_enter_rep_all(event):
rep_all_des.place(x=220,y=460)
def on_leave_rep_all(event):
rep_all_des.place(x=1000,y=1000)
repeat_img = PhotoImage(file='resources/repeat.png')
repeat_button = Button(win, image=repeat_img,bd=0,command=lambda:self.set_con(2))
repeat_button.place(x=200,y=440)
repeat_button.bind('<Enter>',on_enter_rep_all)
repeat_button.bind('<Leave>',on_leave_rep_all)
def on_enter_rep_one(event):
rep_one_des.place(x=250,y=460)
def on_leave_rep_one(event):
rep_one_des.place(x=1000,y=1000)
rep_one_img = PhotoImage(file='resources/rep_one.png')
rep_one_button = Button(win, image=rep_one_img,bd=0,command=lambda:self.set_con(3))
rep_one_button.place(x=230,y=437)
rep_one_button.bind('<Enter>',on_enter_rep_one)
rep_one_button.bind('<Leave>',on_leave_rep_one)
play_des = Label(win, text='Play/Pause',relief='groove')
prev_des = Label(win, text='Previous Track',relief='groove')
stop_des = Label(win, text='Stop Music',relief='groove')
next_des = Label(win, text='Next Track',relief='groove')
shuffle_des = Label(win, text='Shuffle All',relief='groove')
rep_all_des = Label(win, text='Repeat All',relief='groove')
rep_one_des = Label(win, text='Repeat One',relief='groove')
vol_des = Label(win, text='Adjust Volume',relief='groove')
## Volume Scale - adjust volume
global scale
scale = ttk.Scale(win, from_=0, to=100, orient=HORIZONTAL,command=self.set_vol)
scale.set(70) # implement the default value of scale when music player starts
mixer.music.set_volume(0.7)
scale.place(x=680,y=440)
## Time Durations
global dur_start, dur_end
dur_start = Label(win, text='--:--',font=('Calibri',10,'bold'))
dur_start.place(x=5,y=400)
dur_end = Label(win, text='--:--',font=('Calibri',10,'bold'))
dur_end.place(x=750,y=400)
## Progress Bar - The progress bar which indicates the running music
global progress_bar
progress_bar = ttk.Progressbar(win, orient='horizontal',length=705)
progress_bar.place(x=42,y=400)
## Status Bar - at the bottom of window
global status_bar
status_bar = Label(win,text='Welcome to DeePlayer',relief_='sunken',anchor=W)
status_bar.pack(side=BOTTOM,fill=X)
win.protocol("WM_DELETE_WINDOW", self.exit)
win.mainloop()
music_player = Main_class()