forked from Shubhanshu1902/mp3player
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrial.py
More file actions
303 lines (215 loc) · 11.4 KB
/
trial.py
File metadata and controls
303 lines (215 loc) · 11.4 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
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
from tkinter import *
import pygame
from tkinter.messagebox import showinfo
from tkinter import filedialog
import time
import mutagen.mp3
from mutagen.mp3 import MP3
import tkinter.ttk as ttk
root = Tk()
root.title('player')
icon=PhotoImage(file=r"icons/MP3.png")
root.iconphoto(False,icon)
root.geometry("1280x700")
root.configure(bg='#191414')
pygame.mixer.init()
# Defining Function to Get length and time information about current song
def song_time():
# Current Position of song in seconds (Dividing by thousand as default is milliseconds)
current_time = pygame.mixer.music.get_pos() / 1000
# Converting given time to SPECIFIC FORMAT (more formal way H:M:S here)
formal_time = time.strftime('%M:%S', time.gmtime(current_time))
# Now Finding Current Song
song = playlist.get(ACTIVE) # Grab song title from playlist using ACTIVE that represents current song here
song = f'E:/Python_project/Songs/{song}.mp3' # Adding extra removed stuffs of path of a song
# Now Finding Length Of A song using Mutagen after getting current song as above
song_in_mut = MP3(song) # Passing song in mutagen and loading it with module to find it's Length
song_len = song_in_mut.info.length # This will return us the length of selected song in seconds
# Now converting the time we got in seconds to M:S form
song_length = time.strftime('%M:%S', time.gmtime(song_len))
# Output time and song length to show on screen using config
status_bar.config(text=f" Song Duration: {formal_time} / {song_length} ")
# Now we want to do this every time our new song starts playing so calling this song_time in play
# Now updating current_time of song every single second(1000 milliseconds) till it's Playing that is done by after
# Basically like looping(i.e Calling function every single second till length of song)
status_bar.after(1000, song_time)
# Defining Remove A Song Function in Add Option in Main Menu
def remove_song(): # Removes a selected one
# Removing the Highlighted Song (i.e. here so called ANCHORED SONG)
playlist.delete(ANCHOR)
# After deleting the song it must stop playing it so we stop the song here (if playing)
pygame.mixer.music.stop()
# Defining Remove Many Songs Function in Add Option in Main Menu
def remove_all_songs(): # Removes all
# Passing All Songs(we selected before in playlist) at once to delete using range form (0, till END)
playlist.delete(0,END)
# Stop playing any song (if its playing)
pygame.mixer.music.stop()
# Defining Add A Song Function in Add Option in Main Menu
def add_song():
# To Open files to select songs from any directory
song = filedialog.askopenfilename(title="Select One Song" , filetypes=(("MP3 Files", "*.mp3"), ))
# Adding one other variable to give our songs whole path to it
temp_song=song
# To Remove Extra Stuffs Getting printed While Adding Song Name in Queue
h=-1
for i in range(len(song)):
if(song[h]=="/"):
song = song.replace(song[0:(h+1)], "")
song = song.replace(".mp3", "")
break
else:
h=h-1
# Adding Song To playlist
playlist.insert(END, song)
# Defining Add Many Songs Function in Add Option in Main Menu
def add_many_songs():
songs = filedialog.askopenfilenames(title="Select Many Songs" , filetypes=(("MP3 Files", "*.mp3"), ))
# Giving paths of all songs in tuple to a temporary variable so as to access the whole path of any song from anywhere
temp_songs=songs
# Assigning temporary variable to every song in songs
for temp_song in temp_songs:
temp_song=temp_song
# As Add Many Songs Is just Repetiton Of What We Did In Add A Song, We will Do That Things in loop
for song in songs:
# To Remove Extra Stuffs Getting printed While Adding Song Name in Queue
h=-1
for i in range(len(song)):
if(song[h]=="/"):
song = song.replace(song[0:(h+1)], "")
song = song.replace(".mp3", "")
break
else:
h=h-1
# Adding Song To playlist
playlist.insert(END, song)
# Defining Help Button's Function
def Help():
# Showinginfo is a command to display written things on Screen inside tkinter.messagebox, whose syntax is (Label, Message to be shown)
showinfo("MP3 PLAYER", "Contact ESS112_GROUP-1 For Doubts Related To This Code")
# Defining About Button's Function
def About():
# Showinginfo is a command to display written things on Screen inside tkinter.messagebox, whose syntax is (Label, Message to be shown)
showinfo("MP3 PLAYER", "MP3 PLAYER by ESS112_GROUP-1")
# Defining Volume Function to do it's work
# To See The level Of Volume Stretch from below or MP3 Player to see volume there
# pos here holds the value that where basically the volume slider is there
def Volume(pos):
# Using this command we can increase volume from above to down
# MAX value at Bottom is 1 and Above is 0
pygame.mixer.music.set_volume(volume_slider.get())
# Given Below Part is used in play but is a part of Volume slider, so added here as comments
# We here gave Curvol as it shows The Current Volume while we play any song after being loaded
# Curvol shows Current volume here
# curvol = pygame.mixer.music.get_volume()
# volume_slider_label.config(text=curvol * 100) # Multiplied by 100 as volume by default is shown in floating points using pygame
# Giving Works To Every Buttons
# Defining Play Button
def Play():
# To Load Selected Song
song = playlist.get(ACTIVE)
# Adding Extra Part Of Path Of Function As No Song will be played just by its name
song = f'E:/Python_project/Songs/{song}.mp3'
# Playing song with the help of pygame
pygame.mixer.music.load(song)
pygame.mixer.music.play(loops=0)
# Calling song_time function in Play
song_time()
# We here gave Curvol as it shows The Current Volume while we play any song after being loaded
# Curvol shows Current volume here
curvol = pygame.mixer.music.get_volume()
volume_slider_label.config(text=curvol * 100) # Multiplied by 100 as volume by default is shown in floating points using pygame
volume_slider_label["bg"]= "red" # Setting Red colour to background where it shows text(volume level)
volume_slider_label["fg"]= "white" # Setting white colour to text shown
# Create Check Variable To Check Whether A Song Is Running Or Not
global Check
Check = False
# Defining Pause Button
def Pause(is_paused):
# Using Global Variable Here So that Every Time We Pause Or Unpause A Song, The Value Of The "Check" Variable Changes, allowing us to work properly with our player
global Check
Check = is_paused
# Pausing A Song
if Check==False:
# Used Direct Command with Mixer Module To Pause Song
pygame.mixer.music.pause()
# Changing "Check" Variable's value to True tell "SONG IS PAUSED NOW"
Check = True
# Unpausing A Song
else:
# Used Direct Command with Mixer Module To UnPause Song
pygame.mixer.music.unpause()
# Changing "Check" Variable's value to False tell "SONG IS UNPAUSED NOW"
Check = False
# Defining Forward Button
def Forward():
# Converting Songs To Tuples here using curselection so to know which song is being played
# Basically here Songs Are Numbered
# Curselection is Current Selection To know which song is being played from given list of tuples of songs
next_song = playlist.curselection()
# Now Adding One To Current Song number from tuples to Select "NEXT" song from Tuple of songs(OR Order in which we selected the songs)
next_song = next_song[0]+1
# Getting The Song Corresponding To Number In Tuple
song = playlist.get(next_song)
# Adding Extra Part Of Path Of Function As No Song will be played just by its name
song = f'E:/Python_project/Songs/{song}.mp3'
# Now After Selecting The Next Song By Above Steps, We'll Play THE NEXT SONG
# Playing song with the help of pygame
pygame.mixer.music.load(song)
pygame.mixer.music.play(loops=0)
# Now to Move Selection Line(Showing Current Song) to Next Song in playlist by clearing it from Current song and Make it appear on Next Song
# So, clearing bar From Current Song here.
playlist.selection_clear(0, END)
# Making Appear(Activating) Selection Line On Next Song After clearing it from current song
playlist.activate(next_song) # This Will just move underline from current song to next song
# Here, we did last = none means it says we are not highlighting more than one thing in list and just ending highlighting in one element only
playlist.selection_set(next_song, last=None) # This will move highlighter to next song
# Defining Back Button
def Back():
# Not Commenting Back Part As it is just Reverse to what we did in Forward and process is simple
previous_song = playlist.curselection()
previous_song = previous_song[0]-1
song = playlist.get(previous_song)
song = f'E:/Python_project/Songs/{song}.mp3'
pygame.mixer.music.load(song)
pygame.mixer.music.play(loops=0)
playlist.selection_clear(0, END)
playlist.selection_set(previous_song, last=None)
# Defining Stop Button
def Stop():
# Used Direct Command with Mixer Module To Stop Song
pygame.mixer.music.stop()
# Clearing Selection line from current song
playlist.selection_clear(ACTIVE)
# Clearing Status_Bar by writing nothing inside it as, when we use stop as no song will be played after it
status_bar.config(text=" ")
#basic_cmd_frame = Frame(root).grid(row=1, column=0)
master_frame = Frame(root)
master_frame.pack(pady = 30)# pady means padding in y to make it look properly aligned and attractive (same for padx in x direction so not explaining it everywhere)
master_frame['bg'] = 'white'
"""button_frame=Frame(root,bg="black")
button_frame.grid(row=1, column=1)
"""
playlist = Listbox(master_frame, bg="orange", fg="White", width=40, selectbackground='DarkGreen') # Putting our playlist in Master frame
playlist.grid(row=0, column=0)
volume_frame = LabelFrame(master_frame, text="Volume")
volume_frame.grid(row=0, column=1, padx=30)
forward_image = PhotoImage(file=r"icons/resized/fast-forward-button.png")
back_image = PhotoImage(file=r"icons/resized/rewind.png")
stop_image = PhotoImage(file=r"icons/resized/stop-button.png")
pause_image = PhotoImage(file=r"icons/resized/pause_button.png")
play_image = PhotoImage(file=r"icons/resized/play-button.png")
back = Button(root, image=back_image,fg="black", borderwidth=0, command=Back)
forward = Button(root, image=forward_image,fg="black", borderwidth=0, command=Forward)
play = Button(root, image=play_image,fg="black", borderwidth=0, command=Play)
pause = Button(root, image=pause_image,fg="black", borderwidth=0, command=lambda: Pause(Check))
stop = Button(root, image=stop_image,fg="black", borderwidth=0, command=Stop)
back.grid(row=1, column=1, padx=(270,8))
forward.grid(row=1, column=5, padx=8)
play.grid(row=1, column=2, padx=8)
pause.grid(row=1, column=3, padx=8)
stop.grid(row=1, column=4, padx=8)
#shuff_btn = Button(root, image=shuff_btn_img, borderwidth=0).grid(row=1, column=3, padx=40)
#loop_btn = Button(root, image=loop_btn_img, borderwidth=0).grid(row=1, column=7, padx=40)
bf.main()
root.mainloop()