-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
355 lines (297 loc) · 12.9 KB
/
Copy pathmain.py
File metadata and controls
355 lines (297 loc) · 12.9 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
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
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from pygame import mixer
import os
import sys
import random
import threading
import time
import mutagen
from mutagen.id3 import ID3
from mutagen.mp3 import MP3
# TITLE UPDATOR
# RANDOMIZE SONG TOGGLE
# Add a queue system
"""
I DIDN"T USE CLASSES BC IM STUPID AND DONT KNOW HOW TO USE THEM WELL.
I used pygame to intialize the music.
The os library to check if the files in the folder are mp3 or mp4 and to search through the file
PyQt5 to make the gui and assign the Buttons, Slider and ProgressBar
Then more pygame music methods to detect things such as get_length or get_volume.
TO USE
1. Make sure you have pygame installed (pip install pygame) and PyQt5 installed (pip install PyQt5)
2. Add Music Files (.mp3 and .mp4 and .wav files) into the Music Playlist folder.
3. Run and Enjoy.
"""
def get_mutagen(path):
SongData.clear()
audio = ID3(path) #path: path to file
SongData.append(ID3(song_playlist + Music_Names)['TPE1'].text[0]) # Author
SongData.append(audio['TDRC'].text[0]) # Date
total_length = mixer.Sound(path).get_length() # length of the Song in seconds
mins, secs = divmod(total_length, 60)
timeformat = '{:02d}:{:02d}'.format(round(mins), round(secs))
print(timeformat)
global currentSong, status, shuffleStatus
shuffleStatus = False
status = "stopped"
currentSong = 0
song_playlist = "C:/Users/evync/Documents/code/For Fun/Music Player App/Music Playlist/"
Music_Names = []
SongData = []
# Populates the Music File Names into the Music_Names list.
for file in os.listdir(song_playlist[:-1]): # Folder within the folder than holds the songs
if file.endswith(".mp3" or ".mp4" or ".wav"): # Only adds the song if it's a music file
Music_Names.append(str(file))
#path: path to file
# Prints out all the tags
# TIT2 is the title of the song
# TPE1 is the author of the song
# they are keys of a dictionary
def main():
global status
global currentSong
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle('Music Player')
window.setGeometry(0,0, 500, 500)
window.setWindowIcon(QIcon('C:/Users/evync/Documents/code/For Fun/Music Player App/musicplayer.png'))
window.move(710, 290)
window.setStyleSheet(
'background-color: black;')
window.setWindowFlags(Qt.WindowStaysOnTopHint)
if len(Music_Names) == 0:
print("Make sure there are songs in the folder Or the paths are all correct.")
sys.exit(app.exec_())
mixer.init()
mixer.music.load(song_playlist + Music_Names[currentSong])
mixer.music.set_volume(0)
mixer.music.play()
mixer.music.pause()
mixer.music.set_volume(0.5)
previous = QPushButton('\u23EA', window)
previous.setGeometry(150, 400, 50, 50)
previous.setStyleSheet(
'color: white;'
'font-size: 45px;')
play_pause = QPushButton('\u23EF', window)
play_pause.setGeometry(225,400,50,50)
play_pause.setStyleSheet(
'border-radius: 16px;'
'font-size: 45px;')
next = QPushButton('\u23E9', window)
next.setGeometry(300,400,50,50)
next.setStyleSheet(
'border-radius: 16px;'
'font-size: 45px;')
shuffle = QPushButton('\U0001F500', window)
shuffle.setGeometry(375,400,50,50)
shuffle.setStyleSheet(
'border-radius: 16px;'
'font-size: 45px;')
restart = QPushButton('\U0001F504', window)
restart.setGeometry(75,400,50,50)
restart.setStyleSheet(
'border-radius: 16px;'
'font-size: 45px;')
lbl = QLabel(window)
lbl.setPixmap(QPixmap('C:/Users/evync/Documents/code/For Fun/Music Player App/musicplayer.png').scaled(50, 50, Qt.KeepAspectRatio, Qt.FastTransformation))
lbl.move(25, 25)
lbl.show()
speaker = QPushButton('\U0001F50A', window)
speaker.setGeometry(425,25,50,50)
speaker.setStyleSheet(
'border-radius: 16px;'
'font-size: 45px;')
volume = QSlider(Qt.Vertical, window)
volume.setGeometry(425, 75, 50, 50)
volume.setMinimum(0)
volume.setMaximum(100)
volume.setValue(50)
volume.setSingleStep(5)
volume.hide()
progressBar = QProgressBar(window)
# progressBar.setMaximumSize(mixer.music.Sound.get_length())
progressBar.setGeometry(75, 375, 350, 10)
progressBar.setStyleSheet(
"border: 2px solid #2196F3;"
"border-radius: 5px;"
"background-color: #E0E0E0;"
)
title = QLabel(ID3(song_playlist + Music_Names[currentSong])['TIT2'].text[0],window)
title.setGeometry(125, 25, 250, 70)
title.setAlignment(Qt.AlignCenter)
title.setWordWrap(True)
title.setStyleSheet(
"color: white;"
"font-size: 20px;"
)
author = QLabel(ID3(song_playlist + Music_Names[currentSong])['TPE1'].text[0],window)
author.setGeometry(125, 100, 250, 30)
author.setAlignment(Qt.AlignCenter)
author.setWordWrap(True)
author.setStyleSheet(
"color: white;"
"font-size: 15px;"
)
# year = QLabel(str(ID3(song_playlist + Music_Names[currentSong])['TDRC'].text[0]) ,window)
# year.setGeometry(125, 135, 250, 25)
# year.setAlignment(Qt.AlignCenter)
# year.setWordWrap(True)
# year.setStyleSheet(
# "color: white;"
# "font-size: 15px;"
# )
shuffle.clicked.connect(lambda: shuffleSong())
play_pause.clicked.connect(lambda: playOrPause())
previous.clicked.connect(lambda: previousSong())
restart.clicked.connect(lambda: restartSong())
next.clicked.connect(lambda: nextSong())
speaker.clicked.connect(lambda: volumeHider())
volume.valueChanged.connect(lambda: VolumeChanged(volume.value()))
def volumeHider():
if volume.isHidden() == True:
volume.show()
elif volume.isHidden() == False:
volume.hide()
def check_if_finished(var): # Threading makes it run constantly inside it loops
global shuffleStatus
global currentSong
while True:
if status == "playing" and mixer.music.get_busy() == False: # Checks if the song is playing (Even though it's over) and the music mixer is off THIS MEANS THAT THE SONG IS DONE
if shuffleStatus == False:
nextSong()
elif shuffleStatus == True:
shuffleSong()
print(mixer.music.get_busy)
# if mixer.music.get_busy():
# print('hi')
# get_mutagen(song_playlist + Music_Names[currentSong])
# print(SongData[0])
# print(SongData[1])
# print(SongData[2])
time.sleep(1)
if var.is_set():
break
event = threading.Event()
thread = threading.Thread(target=check_if_finished, args=(event, )) # Creates the threading
thread.start()
def closer(event):
event.set()
def playOrPause():
global status
if status == "playing":
mixer.music.pause()
title.setText(ID3(song_playlist + Music_Names[currentSong])['TIT2'].text[0]) # Title
author.setText(ID3(song_playlist + Music_Names[currentSong])['TPE1'].text[0]) # Author
# year.setText(str(ID3(song_playlist + Music_Names[currentSong])['TDRC'].text[0])) # year
print('paused')
status = "stopped"
elif status == "stopped":
mixer.music.unpause()
title.setText(ID3(song_playlist + Music_Names[currentSong])['TIT2'].text[0])
author.setText(ID3(song_playlist + Music_Names[currentSong])['TPE1'].text[0])
# year.setText(str(ID3(song_playlist + Music_Names[currentSong])['TDRC'].text[0]))
print('unpaused')
status = "playing"
print(Music_Names[currentSong])
def nextSong():
global currentSong
if shuffleStatus == True:
shuffler()
else:
if currentSong > (len(Music_Names) - 2):
currentSong = 0
else:
currentSong += 1
mixer.music.load(song_playlist + Music_Names[currentSong])
mixer.music.set_volume(0)
mixer.music.play()
mixer.music.set_volume(0.5)
if status == "playing":
mixer.music.play()
print(Music_Names[currentSong])
title.setText(ID3(song_playlist + Music_Names[currentSong])['TIT2'].text[0])
author.setText(ID3(song_playlist + Music_Names[currentSong])['TPE1'].text[0])
# year.setText(str(ID3(song_playlist + Music_Names[currentSong])['TDRC'].text[0]))
else:
mixer.music.pause()
title.setText(ID3(song_playlist + Music_Names[currentSong])['TIT2'].text[0])
author.setText(ID3(song_playlist + Music_Names[currentSong])['TPE1'].text[0])
# year.setText(str(ID3(song_playlist + Music_Names[currentSong])['TDRC'].text[0]))
def previousSong():
global currentSong
if shuffleStatus == True:
shuffler()
else:
if currentSong == 0:
currentSong = len(Music_Names) - 1
else:
currentSong -= 1
mixer.music.load(song_playlist + Music_Names[currentSong])
mixer.music.set_volume(0)
mixer.music.play()
mixer.music.set_volume(0.5)
if status == "playing":
mixer.music.play()
print(Music_Names[currentSong])
title.setText(ID3(song_playlist + Music_Names[currentSong])['TIT2'].text[0])
author.setText(ID3(song_playlist + Music_Names[currentSong])['TPE1'].text[0])
# year.setText(str(ID3(song_playlist + Music_Names[currentSong])['TDRC'].text[0]))
else:
mixer.music.pause()
title.setText(ID3(song_playlist + Music_Names[currentSong])['TIT2'].text[0])
author.setText(ID3(song_playlist + Music_Names[currentSong])['TPE1'].text[0])
# year.setText(str(ID3(song_playlist + Music_Names[currentSong])['TDRC'].text[0]))
def restartSong():
global currentSong
mixer.music.load(song_playlist + Music_Names[currentSong])
mixer.music.set_volume(0)
mixer.music.play()
mixer.music.set_volume(0.5)
if status == "playing":
mixer.music.play()
print(Music_Names[currentSong])
title.setText(ID3(song_playlist + Music_Names[currentSong])['TIT2'].text[0])
author.setText(ID3(song_playlist + Music_Names[currentSong])['TPE1'].text[0])
# year.setText(str(ID3(song_playlist + Music_Names[currentSong])['TDRC'].text[0]))
else:
mixer.music.pause()
title.setText(ID3(song_playlist + Music_Names[currentSong])['TIT2'].text[0])
author.setText(ID3(song_playlist + Music_Names[currentSong])['TPE1'].text[0])
# year.setText(str(ID3(song_playlist + Music_Names[currentSong])['TDRC'].text[0]))
def shuffleSong():
global shuffleStatus
if shuffleStatus == True:
shuffleStatus = False
elif shuffleStatus == False:
shuffleStatus = True
def shuffler():
global currentSong
randomInt = random.randint(0,(len(Music_Names) - 1))
if randomInt == currentSong:
randomInt = random.randint(0,(len(Music_Names) - 1))
currentSong = randomInt
mixer.music.load(song_playlist + Music_Names[currentSong])
mixer.music.set_volume(0)
mixer.music.play()
mixer.music.set_volume(0.5)
if status == "playing":
mixer.music.play()
print(Music_Names[currentSong])
title.setText(ID3(song_playlist + Music_Names[currentSong])['TIT2'].text[0])
author.setText(ID3(song_playlist + Music_Names[currentSong])['TPE1'].text[0])
# year.setText(str(ID3(song_playlist + Music_Names[currentSong])['TDRC'].text[0]))
else:
mixer.music.pause()
title.setText(ID3(song_playlist + Music_Names[currentSong])['TIT2'].text[0])
author.setText(ID3(song_playlist + Music_Names[currentSong])['TPE1'].text[0])
# year.setText(str(ID3(song_playlist + Music_Names[currentSong])['TDRC'].text[0]))
def VolumeChanged(volume):
mixer.music.set_volume(volume/50)
app.aboutToQuit.connect(lambda: closer(event))
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()