-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathplayer.py
304 lines (233 loc) · 8.48 KB
/
player.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
from tkinter import *
from tkinter import filedialog
import pygame
import time
from mutagen.mp3 import MP3
import tkinter.ttk as ttk
root = Tk()
root.title("MP3 Player")
root.geometry("500x400")
# Initialize Pygame
pygame.mixer.init()
# Create Function To Deal With Time
def play_time():
# Check to see if song is stopped
if stopped:
return
# Grab Current Song Time
current_time = pygame.mixer.music.get_pos() / 1000
# Convert Song Time To Time Format
converted_current_time = time.strftime('%M:%S', time.gmtime(current_time))
# Reconstruct song with directory structure stuff
song = playlist_box.get(ACTIVE)
song = f'C:/mp3/audio/{song}.mp3'
# Find Current Song Length
song_mut = MP3(song)
global song_length
song_length = song_mut.info.length
# Convert to time format
converted_song_length = time.strftime('%M:%S', time.gmtime(song_length))
# Check to see if song is over
if int(song_slider.get()) == int(song_length):
stop()
elif paused:
# Check to see if paused, if so - pass
pass
else:
# Move slider along 1 second at a time
next_time = int(song_slider.get()) + 1
# Output new time value to slider, and to length of song
song_slider.config(to=song_length, value=next_time)
# Convert Slider poition to time format
converted_current_time = time.strftime('%M:%S', time.gmtime(int(song_slider.get())))
# Output slider
status_bar.config(text=f'Time Elapsed: {converted_current_time} of {converted_song_length} ')
# Add Current Time To Status Bar
if current_time > 0:
status_bar.config(text=f'Time Elapsed: {converted_current_time} of {converted_song_length} ')
# Create Loop To Check the time every second
status_bar.after(1000, play_time)
# Create Function To Add One Song To Playlist
def add_song():
song = filedialog.askopenfilename(initialdir='audio/', title="Choose A Song", filetypes=(("mp3 Files", "*.mp3" ), ))
# Strip out directory structure and .mp3 from song title
song = song.replace("C:/mp3/audio/", "")
song = song.replace(".mp3", "")
# Add To End of Playlist
playlist_box.insert(END, song)
# Create Function To Add Many Songs to Playlist
def add_many_songs():
songs = filedialog.askopenfilenames(initialdir='audio/', title="Choose A Song", filetypes=(("mp3 Files", "*.mp3" ), ))
# Loop thru song list and replace directory structure and mp3 from song name
for song in songs:
# Strip out directory structure and .mp3 from song title
song = song.replace("C:/mp3/audio/", "")
song = song.replace(".mp3", "")
# Add To End of Playlist
playlist_box.insert(END, song)
# Create Function To Delete One Song From Playlist
def delete_song():
# Delete Highlighted Song From Playlist
playlist_box.delete(ANCHOR)
# Create Function To Delete All Songs From Playlist
def delete_all_songs():
# Delete ALL songs
playlist_box.delete(0, END)
# Create Play Function
def play():
# Set Stopped to False since a song is now playing
global stopped
stopped = False
# Reconstruct song with directory structure stuff
song = playlist_box.get(ACTIVE)
song = f'C:/mp3/audio/{song}.mp3'
#Load song with pygame mixer
pygame.mixer.music.load(song)
#Play song with pygame mixer
pygame.mixer.music.play(loops=0)
# Get Song Time
play_time()
# Create Stopped Variable
global stopped
stopped = False
def stop():
# Stop the song
pygame.mixer.music.stop()
# Clear Playlist Bar
playlist_box.selection_clear(ACTIVE)
status_bar.config(text='')
# Set our slider to zero
song_slider.config(value=0)
# Set Stop Variable To True
global stopped
stopped = True
# Create Function To Play The Next Song
def next_song():
# Reset Slider position and status bar
status_bar.config(text='')
song_slider.config(value=0)
#Get current song number
next_one = playlist_box.curselection()
# Add One To The Current Song Number Tuple/list
next_one = next_one[0] + 1
# Grab the song title from the playlist
song = playlist_box.get(next_one)
# Add directory structure stuff to the song title
song = f'C:/mp3/audio/{song}.mp3'
#Load song with pygame mixer
pygame.mixer.music.load(song)
#Play song with pygame mixer
pygame.mixer.music.play(loops=0)
# Clear Active Bar in Playlist
playlist_box.selection_clear(0, END)
# Move active bar to next song
playlist_box.activate(next_one)
# Set Active Bar To next song
playlist_box.selection_set(next_one, last=None)
# Create function to play previous song
def previous_song():
# Reset Slider position and status bar
status_bar.config(text='')
song_slider.config(value=0)
#Get current song number
next_one = playlist_box.curselection()
# Add One To The Current Song Number Tuple/list
next_one = next_one[0] - 1
# Grab the song title from the playlist
song = playlist_box.get(next_one)
# Add directory structure stuff to the song title
song = f'C:/mp3/audio/{song}.mp3'
#Load song with pygame mixer
pygame.mixer.music.load(song)
#Play song with pygame mixer
pygame.mixer.music.play(loops=0)
# Clear Active Bar in Playlist
playlist_box.selection_clear(0, END)
# Move active bar to next song
playlist_box.activate(next_one)
# Set Active Bar To next song
playlist_box.selection_set(next_one, last=None)
# Create Paused Variable
global paused
paused = False
# Create Pause Function
def pause(is_paused):
global paused
paused = is_paused
if paused:
#Unpause
pygame.mixer.music.unpause()
paused = False
else:
#Pause
pygame.mixer.music.pause()
paused = True
#Create Volume Function
def volume(x):
pygame.mixer.music.set_volume(volume_slider.get())
# Create a Slide Function For Song Positioning
def slide(x):
# Reconstruct song with directory structure stuff
song = playlist_box.get(ACTIVE)
song = f'C:/mp3/audio/{song}.mp3'
#Load song with pygame mixer
pygame.mixer.music.load(song)
#Play song with pygame mixer
pygame.mixer.music.play(loops=0, start=song_slider.get())
# Create main Frame
main_frame = Frame(root)
main_frame.pack(pady=20)
# Create Playlist Box
playlist_box = Listbox(main_frame, bg="black", fg="green", width=60, selectbackground="green", selectforeground='black')
playlist_box.grid(row=0, column=0)
# Create volume slider frame
volume_frame = LabelFrame(main_frame, text="Volume")
volume_frame.grid(row=0, column=1, padx=20)
# Create Volume Slider
volume_slider = ttk.Scale(volume_frame, from_=0, to=1, orient=VERTICAL, length=125, value=1, command=volume)
volume_slider.pack(pady=10)
# Create Song Slider
song_slider = ttk.Scale(main_frame, from_=0, to=100, orient=HORIZONTAL, length=360, value=0, command=slide)
song_slider.grid(row=2, column=0, pady=20)
# Define Button Images For Controls
back_btn_img = PhotoImage(file='images/back50.png')
forward_btn_img = PhotoImage(file='images/forward50.png')
play_btn_img = PhotoImage(file='images/play50.png')
pause_btn_img = PhotoImage(file='images/pause50.png')
stop_btn_img = PhotoImage(file='images/stop50.png')
# Create Button Frame
control_frame = Frame(main_frame)
control_frame.grid(row=1, column=0, pady=20)
# Create Play/Stop etc Buttons
back_button = Button(control_frame, image=back_btn_img, borderwidth=0, command=previous_song)
forward_button = Button(control_frame, image=forward_btn_img, borderwidth=0, command=next_song)
play_button = Button(control_frame, image=play_btn_img, borderwidth=0, command=play)
pause_button = Button(control_frame, image=pause_btn_img, borderwidth=0, command=lambda: pause(paused))
stop_button = Button(control_frame, image=stop_btn_img, borderwidth=0, command=stop)
back_button.grid(row=0, column=0, padx=10)
forward_button.grid(row=0, column=1, padx=10)
play_button.grid(row=0, column=2, padx=10)
pause_button.grid(row=0, column=3, padx=10)
stop_button.grid(row=0, column=4, padx=10)
# Create Main Menu
my_menu = Menu(root)
root.config(menu=my_menu)
# Create Add Song Menu Dropdows
add_song_menu = Menu(my_menu, tearoff=0)
my_menu.add_cascade(label="Add Songs", menu=add_song_menu)
# Add One Song To Playlist
add_song_menu.add_command(label="Add One Song To Playlist", command=add_song)
# Add Many Songs to Playlist
add_song_menu.add_command(label="Add Many Songs To Playlist", command=add_many_songs)
# Create Delete Song Menu Dropdowns
remove_song_menu = Menu(my_menu, tearoff=0)
my_menu.add_cascade(label="Remove Songs", menu=remove_song_menu)
remove_song_menu.add_command(label="Delete A Song From Playlist", command=delete_song)
remove_song_menu.add_command(label="Delete All Songs From Playlist", command=delete_all_songs)
# Create Status Bar
status_bar = Label(root, text='', bd=1, relief=GROOVE, anchor=E)
status_bar.pack(fill=X, side=BOTTOM, ipady=2)
# Temporary Label
my_label = Label(root, text='')
my_label.pack(pady=20)
root.mainloop()