-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSound_Player.py
More file actions
182 lines (155 loc) · 6.16 KB
/
Copy pathSound_Player.py
File metadata and controls
182 lines (155 loc) · 6.16 KB
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
import pygame
from tkinter import *
from tkinter import filedialog
from tkinter import Menu
from tkinter.ttk import *
from mutagen.mp3 import MP3
import time
import threading
window = Tk()
window_icon = PhotoImage(file="window_icon.png")
window.iconphoto(False,window_icon)
sound_list = []
playing_sound = False
pygame.mixer.init()
audio_mutagen = None
current_time = 0
first_time = True
volume_label = Label(window, text="Volume")
volume_label.place(rely=0.98,relx=0.02,anchor=SW)
full_volume_icon = PhotoImage(file="full_volume_icon.png")
low_volume_icon = PhotoImage(file="low_volume_icon.png")
mute_volume_icon = PhotoImage(file="mute_volume_icon.png")
def open():
files1 = filedialog.askopenfilenames(filetypes=[("Mp3 Files", "*.mp3")])
files = list(files1)
sound_list_in_list = sound_list_box.curselection()
for audio1 in files:
audio = audio1.replace(' ','%20')
sound_list.append(audio)
sound_list_box.insert(len(sound_list)+files.index(audio1),'🎵'+audio1.split('/')[-1].replace('.'+audio1.split('.')[-1],' '))
play_sound.configure(state="normal")
delete_sound.configure(state="normal")
def play():
global playing_sound,current_time
sound_list_in_list = sound_list_box.curselection()
for sound in sound_list_in_list:
pygame.mixer.music.unload()
pygame.mixer.music.load(sound_list[sound].replace('%20',' '))
pygame.mixer.music.play()
playing_sound = True
pause_sound.configure(state="normal",text="Pause")
pause_sound.configure(image=pause_icon)
global audio_mutagen,current_time,first_time
audio_mutagen = None
audio_mutagen = MP3(sound_list[sound].replace('%20',' '))
total_length = audio_mutagen.info.length
mins, secs = divmod(total_length, 60)
mins = round(mins)
secs = round(secs)
total_seconds = (mins*60)+secs
song_time.configure(from_=0,to=total_seconds)
song_time.state(["!disabled"])
song_time.set(0)
timeformat = '{:02d}:{:02d}'.format(mins, secs)
song_full_time.configure(text=timeformat)
song_now_time.configure(text="00:00")
current_time = 0
if first_time is True:
t1 = threading.Thread(target=start_count, args=(total_length,))
t1.start()
first_time = False
def start_count(t):
global playing_sound,current_time
while current_time <= t and pygame.mixer.music.get_busy():
if playing_sound == False:
continue
else:
mins, secs = divmod(current_time, 60)
mins = round(mins)
secs = round(secs)
timeformat = '{:02d}:{:02d}'.format(mins, secs)
song_now_time.configure(text = timeformat)
song_time.set(current_time)
time.sleep(1)
current_time += 1
def pause():
global playing_sound
if playing_sound:
pygame.mixer.music.pause()
playing_sound = False
pause_sound.configure(text="Resume")
pause_sound.configure(image=resume_icon)
song_time.state(["disabled"])
else:
pygame.mixer.music.unpause()
playing_sound = True
pause_sound.configure(text="Pause")
pause_sound.configure(image=pause_icon)
song_time.state(["!disabled"])
def delete():
sound_list_in_list = sound_list_box.curselection()
for sound in sound_list_in_list:
sound_list_box.delete(sound)
sound_list.remove(sound_list[sound])
if len(sound_list) == 0:
global t1
play_sound.configure(state="disabled")
pause_sound.configure(state="disabled",text="Pause")
pause_sound.configure(image=pause_icon)
delete_sound.configure(state="disabled")
song_time.configure(from_=0,to=0)
song_time.state(['disabled'])
song_full_time.configure(text='00:00')
song_now_time.configure(text='00:00')
playing_sound = False
pygame.mixer.music.unload()
def volume(value):
pygame.mixer.music.set_volume(float(value)/100)
value = int(float(value))
global volume_label
if value < 101 and value > 75:
volume_label.configure(image=full_volume_icon)
elif value<76 and value>0:
volume_label.configure(image=low_volume_icon)
else:
volume_label.configure(image=mute_volume_icon)
def change_time(value):
global current_time
if current_time is not int(float(value)):
pygame.mixer.music.play(-1, int(float(value)))
current_time = int(float(value))
open_files = Button(window,text="Open Files", command=open)
open_files.place(rely=0.98, relx=0.95, anchor=SE)
open_icon = PhotoImage(file="open_icon.png")
open_files.configure(image=open_icon,compound = LEFT)
play_sound = Button(window,text="Play",command=play, state="disabled")
play_sound.place(rely=0.98,relx=0.45,anchor=S)
play_icon = PhotoImage(file="play_icon.png")
play_sound.configure(image=play_icon)
pause_sound = Button(window,text="Pause",command=pause, state="disabled")
pause_sound.place(rely=0.98,relx=0.30,anchor=S)
pause_icon = PhotoImage(file="pause_icon.png")
resume_icon = PhotoImage(file="resume_icon.png")
pause_sound.configure(image=pause_icon)
delete_sound = Button(window,text="Delete",command=delete, state="disabled")
delete_sound.place(rely=0.98,relx=0.60,anchor=S)
delete_icon = PhotoImage(file="delete_icon.png")
delete_sound.configure(image=delete_icon)
volume_sound = Scale(window,from_=0,to=100,command=volume,orient=HORIZONTAL)
volume_sound.place(rely=0.98,relx=0.055,anchor=SW)
volume_sound.set(100)
volume_label.configure(image=full_volume_icon)
sound_list_box = Listbox(window,width=71,foreground=("#%02x%02x%02x" %(217, 72, 221) ), background="yellow")
sound_list_box.place(relx=0.5, rely=0,anchor=N,height=575)
song_full_time = Label(window,text="00:00")
song_full_time.place(relx=0.95,rely=0.93,anchor=SE)
song_now_time = Label(window,text="00:00")
song_now_time.place(relx=0.020,rely=0.93,anchor=SW)
song_time = Scale(window,from_=0,to=10,orient=HORIZONTAL,command=change_time)
song_time.place(relx=0.485,rely=0.93,anchor=S,width=517,height=20)
song_time.state(['disabled'])
window.title('Music Player')
window.geometry('650x650')
window.resizable(False, False)
window.mainloop()