-
Notifications
You must be signed in to change notification settings - Fork 1
/
Music_player.py
182 lines (158 loc) · 5.59 KB
/
Music_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
# ---------------------- Included packages -------------
import io
from time import sleep
import tkinter as tk
from tkinter import messagebox
import stagger
from MenuBar import MenuBar
from modules import *
from pygame import mixer
from PIL import Image, ImageTk
mixer.init()
# --------------- Global variables ---------------------
volume = 0.5
songs = list()
theme = askForThemeFile()
# --------------- Function definitions -----------------
def changePath():
"""Changes the folder path in which all the mp3 files are present.
Args:
e (event): event variable for mouse click
"""
global pos
pos = 0.0
setPos(0.0)
path = setPath()
setSongIndex(0)
songs = find_songs(path)
loadSong(songs[askForSongIndexFile()])
play_pauseBtn.config(text="Play")
def changeVolume(event):
"""Increases volume when mouse wheel scrolls in forward direction and
decreases volume when mouse wheel scrolls in backward direction.
Args:
event (event): event variable for mouse wheel
"""
global volume
if event.delta < 0 and volume >= 0.0:
volume -= 0.05
mixer.music.set_volume(volume)
if event.delta > 0 and volume <= 1.0:
volume += 0.05
mixer.music.set_volume(volume)
root.title("Volume: "+str(int(volume*10)))
sleep(0.5)
root.title("Music Player")
def loadSong(path):
"""Loads a mp3 files present in the path and displays meta data
into the GUI
Args:
path (Str): exact path of a mp3 file
"""
try:
audio = stagger.read_tag(path)
name.config(text=audio.title)
album.config(text=audio.album)
artist.config(text=audio.artist)
by_data = audio[stagger.id3.APIC][0].data
im = io.BytesIO(by_data)
photo = ImageTk.PhotoImage(Image.open(im).resize((400, 400)))
img.configure(image=photo)
img.image = photo
except:
name.config(text='title unavailable')
album.config(text='album unavailable')
artist.config(text='artist unavailable')
photo = ImageTk.PhotoImage(Image.open(
"resources\images\defaultImage.jpg").resize((400, 400)))
img.configure(image=photo)
img.image = photo
try:
mixer.music.load(path)
mixer.music.play(start=pos)
except:
messagebox.showerror(
"Invalid file", icon='error')
setSongIndex((askForSongIndexFile() + 1) % len(songs))
loadSong(songs[askForSongIndexFile()])
mixer.music.pause()
def prevSong():
"""Plays the previous song
"""
global pos
setSongIndex((askForSongIndexFile() - 1 + len(songs)) % len(songs))
pos = 0.0
loadSong(songs[askForSongIndexFile()])
play_pauseBtn.config(text="Pause")
mixer.music.play()
def nextSong():
"""Plays the next song
"""
global pos
setSongIndex((askForSongIndexFile() + 1) % len(songs))
pos = 0.0
loadSong(songs[askForSongIndexFile()])
play_pauseBtn.config(text="Pause")
mixer.music.play()
def play_Pause():
"""Play and pause toggler
"""
if(mixer.music.get_busy()):
mixer.music.pause()
play_pauseBtn.config(text="Play")
else:
play_pauseBtn.config(text="Pause")
mixer.music.unpause()
# ------------------- Design statements -------------------
root = tk.Tk()
root.title("Music Player")
root.geometry("800x430")
root.maxsize(800, 430)
root.minsize(800, 430)
# ------------------------ Design statements for menu bar ------------------
f0 = tk.Frame(root)
f0.pack(fill=tk.X)
f1 = tk.Frame(f0, bg=f"{theme}")
f1.pack(side=tk.LEFT, fill=tk.BOTH)
photo = ImageTk.PhotoImage(Image.open(
"resources\images\defaultImage.jpg").resize((400, 400)))
img = tk.Label(f1, image=photo)
img.pack(fill=tk.BOTH)
img.bind('<MouseWheel>',changeVolume)
f2 = tk.Frame(f0, bg=f"{theme}")
f2.pack(side=tk.LEFT, fill=tk.BOTH)
f2.bind('<MouseWheel>',changeVolume)
name = tk.Label(f2, bg=f"{theme}", fg=f"{'white' if theme == 'black' else 'black'}", font=("Bahnschrift Condensed", 12))
name.pack(side=tk.TOP, pady=40, fill=tk.X)
album = tk.Label(f2, bg=f"{theme}", fg=f"{'white' if theme == 'black' else 'black'}",
font=("Bahnschrift Condensed", 12))
album.pack(side=tk.TOP, pady=40, fill=tk.X)
artist = tk.Label(f2, bg=f"{theme}", fg=f"{'white' if theme == 'black' else 'black'}",
font=("Bahnschrift Condensed", 12))
artist.pack(side=tk.TOP, pady=40, fill=tk.X)
tk.Button(f2, text="<<", padx=20, pady=10,
command=prevSong,bg=f"{theme}",fg=f"{'white' if theme == 'black' else 'black'}").pack(side=tk.LEFT, padx=30)
play_pauseBtn = tk.Button(f2, text="Play", padx=20,
pady=10, command=play_Pause,bg=f"{theme}",fg=f"{'white' if theme == 'black' else 'black'}")
play_pauseBtn.pack(side=tk.LEFT, padx=30)
tk.Button(f2, text=">>", padx=20, pady=10,
command=nextSong,bg=f"{theme}",fg=f"{'white' if theme == 'black' else 'black'}").pack(side=tk.LEFT, padx=30)
statusBar = tk.Label(root, text="Made with 💟 by Ritwick",
relief=tk.SUNKEN, font=("Bahnschrift Condensed", 12))
statusBar.pack(side=tk.BOTTOM, fill=tk.X)
# ---------------------- Entry Point ------------------
path = askForPath()
songs = find_songs(path)
pos = askForPos()
try:
loadSong(songs[askForSongIndexFile()])
except:
messagebox.showerror(
"Try to Restart me", "Somthing went wrong", icon="error")
os.remove('resources\pathFile.txt')
os.remove('resources\posFile.txt')
os.remove('resources\songIndexFile.txt')
exit(-1)
MenuBar(root, f0, changePath, songs, nextSong, theme)
root.mainloop()
storePos(root, pos, askForSongIndexFile())