forked from Shubhanshu1902/mp3player
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuttonfunc.py
More file actions
290 lines (213 loc) · 12.3 KB
/
buttonfunc.py
File metadata and controls
290 lines (213 loc) · 12.3 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
import tkinter as tk
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
import playlist as pl
pygame.mixer.init()
# Defining Functi 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(tk.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(tk.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, tk.END)
# Stop playing any song (if its playing)
pygame.mixer.music.stop()
temp_song=[]
# 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=(("WAV Files", "*.wav"),))
global temp_song
# Adding one other variable to give our songs whole path to it
temp_song.append(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(".wav", "")
break
else:
h=h-1
# Adding Song To playlist
playlist.insert(tk.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", "*.wav"),))
global temp_song
# Giving paths of all songs in tuple to a temporary variable so as to access the whole path of any song from anywhere
temp_song+=list(songs)
# 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(".wav", "")
break
else:
h=h-1
# Adding Song To playlist
playlist.insert(tk.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(tk.ACTIVE)
# Adding Extra Part Of Path Of Function As No Song will be played just by its name
for i in range(len(temp_song)):
# Playing song with the help of pygame
pygame.mixer.music.load(temp_song[i])
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
# 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, tk.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, tk.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(tk.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=" ")
#Creating Master Frame in which our frame (including all buttons and status toolbar, header ) and volume slider will be there
def main(mp3):
master_frame = tk.Frame(mp3)
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' # Changing Overall background colour to white of master frame
# Making playlist of songs
global playlist
global status_bar
global volume_slider_label
global volume_slider
playlist = tk.Listbox(master_frame, bg="orange", fg="White", width=100,height=20, selectbackground='DarkGreen') # Putting our playlist in Master frame
playlist.grid(row=0, column=0) # Assigning row and column as 0 as it reprents first element of master _current_frames
# Defining button images of our mp3 player
#Creating Frame to add buttons to align them in in one line in centre of screen using pack
# Creating Volume Label Frame To Add Volume Slider here to make it look attractive in box and putting volume_frame in master_frame
volume_frame = tk.LabelFrame(master_frame, text="Volume")
# Assigning Volume Part next to mp3_frame of Ours using row=0 and col=1
volume_frame.grid(row=0, column=1, padx=8)
# Creating Status Bar
# Relief is border-type, ipady is internal padding in y
status_bar = tk.Label(mp3, text='ENJOY MUSIC ', borderwidth=1, relief=tk.SUNKEN, anchor=tk.E)
status_bar.pack(fill=tk.X, side=tk.BOTTOM, ipady=3)
# Creating Volume Slider To increase and decrease volume and putting it in volume frame to look more great
# Orienting it in vertical direction
# Did 0 to 1 as volume in pygame will be given in decimals like 0.001 and all,.. so MAX volume shows 1 here
# Value here is by default 1 it means song will play at MAX volume and Length is value that how much space will it occupy in vertical direction and assigning that value properly to look more perfect
volume_slider = ttk.Scale(volume_frame, from_=0, to=1, orient=tk.VERTICAL, value=1, command=Volume, length=200)
# PAcking slider in volume_frame
volume_slider.pack(pady=10) # Here padded in Y direction so to look more Attractive with spaces above and below of length (10)
volume_slider_label = tk.Label(mp3, text="0") # Shows initial text = 0
volume_slider_label.pack(pady=10)
slider = ttk.Scale(mp3, from_=0, to=100, length=500, orient=tk.HORIZONTAL)
slider.pack(side='bottom')
# Entering in event loop and allowing all the data we entered above to appear on screen