-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMusicPlayer.py
222 lines (169 loc) · 7.7 KB
/
MusicPlayer.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
# Author: Kwame Addo
# File: MusicPlayer.py
import EmotionDetection as EmotionDetection
import Welcome as Welcome
# import everything from tkinter module
import tkinter
from tkinter import *
import os
import pygame
class Window:
def __init__(self, master, mood=None):
self.master = master
self.mood = mood
# self.play_img = PhotoImage(file="play.png")
# self.prev_img = PhotoImage(file="previous.png")
# self.next_img = PhotoImage(file="next(1).png")
# self.pause_img = PhotoImage(file="pause.png")
self.frame = Frame(self.master, bg="#404040")
self.master.geometry("640x480")
self.master.attributes("-fullscreen", True)
self.master.grid_rowconfigure(0, weight=1)
self.master.grid_columnconfigure(0, weight=1)
self.frame_bottom = Frame(self.frame, bg="#404040")
self.prev_button = Button(self.frame_bottom, text="⏮", command=self.prev_music,
borderwidth=0, font=("Arial BOLD", 40), bg="#404040", fg="white")
self.play_button = Button(self.frame_bottom, text="▶", command=self.play_music,
borderwidth=0, font=("Arial BOLD", 40), bg="#404040", fg="white")
self.pause_button = Button(self.frame_bottom, text="⏸", command=self.pause_music,
borderwidth=0, font=("Arial BOLD", 40), bg="#404040", fg="white")
self.next_button = Button(self.frame_bottom, text="⏭", command=self.next_music,
borderwidth=0, font=("Arial BOLD", 40), bg="#404040", fg="white")
self.back_button = Button(self.frame_bottom, text="🔙 Back", command=self.back,
borderwidth=0, font=("Arial BOLD", 40), bg="#404040", fg="white")
self.close_button = Button(self.frame_bottom, text="❌ Quit", command=self.quit,
borderwidth=0, font=("Arial BOLD", 40), bg="#404040", fg="white")
self.song_list = Listbox(self.frame, bg="black", fg="white", width=100, height=15, font=("Consolas BOLD", 25))
self.song_list.pack(fill="both", expand=True)
self.frame.grid(row=0, column=0, sticky=tkinter.NSEW) # restore
# self.frame.pack()
self.frame.tkraise()
pygame.mixer.init()
self.frame_bottom.pack()
self.prev_button.grid(row=0, column=0, padx=7, pady=10)
self.play_button.grid(row=0, column=1, padx=7, pady=10)
self.pause_button.grid(row=0, column=2, padx=7, pady=10)
self.next_button.grid(row=0, column=3, padx=7, pady=10)
self.back_button.grid(row=0, column=4, padx=7, pady=10, sticky="ws")
self.close_button.grid(row=0, column=5,padx =7, pady=10, sticky="ws")
# self.play_button.grid(row=0,column=0)
# self.pause_button.pack()
# self.menu = Menu(master)
self.songs = []
self.pause = False
self.current_song = ""
self.directory = ""
self.load_music()
self.play_music()
def load_music(self):
# music_dir = filedialog.askdirectory()
match self.mood:
case "happiness":
path = "Emotion/happiness"
os.chdir(path)
case "disgust":
path = "Emotion/disgust"
os.chdir(path)
case "fear":
path = "Emotion/fear"
os.chdir(path)
case "sadness":
path = "Emotion/sadness"
os.chdir(path)
case "neutral":
path = "Emotion/neutral"
os.chdir(path)
case "surprise":
path = "Emotion/surprise"
os.chdir(path)
case "anger":
path = "Emotion/anger"
os.chdir(path)
# get the directory containing the mood detected from the camera
music_dir: str = os.getcwd()
# cd directory twice to make sure we static files can be referenced
os.chdir(os.path.dirname(os.getcwd()))
os.chdir(os.path.dirname(os.getcwd()))
print(os.getcwd())
self.directory = music_dir
for song in os.listdir(music_dir):
"""Loops to feel up the songs from the directory to the songs array"""
name, ext = os.path.splitext(song)
if ext == ".mp3" or ext == ".MP3":
self.songs.append(song)
print(self.directory)
# Songs are now added to the Tkinter ListBox
for song in self.songs:
self.song_list.insert("end", song)
self.song_list.select_set(0)
try:
self.current_song = self.songs[self.song_list.curselection()[0]]
print(f" here is the cwd ----{os.getcwd()}")
except:
print("No music")
finally:
print("done")
def print_song(self):
print(self.mood)
def quit(self):
self.master.quit()
def play_music(self, next_check=True) -> None:
""" Play music function"""
if next_check:
self.current_song = self.songs[self.song_list.curselection()[0]]
if not self.pause:
# print(os.path.join(self.directory, self.current_song))
pla = os.path.join(self.directory, self.current_song)
pla = pla.replace("\\", "/")
# print(pla)
self.print_song()
pygame.mixer.music.load(pla)
pygame.mixer.music.play()
self.play_button.configure(bg="light blue",highlightbackground="red")
self.pause_button.configure(bg="#404040", highlightbackground="red")
else:
pygame.mixer.music.unpause()
# self.play_button.configure(bg="#404040", highlightbackground="red")
self.play_button.configure(bg="light blue",highlightbackground="red")
self.pause = False
def pause_music(self) -> None:
"""Setting function for the pause. This causes the music to stop when pause is set to false"""
pygame.mixer.music.pause()
self.pause_button.configure(bg="light blue", highlightbackground="red")
self.play_button.configure(bg="#404040", highlightbackground="red")
self.pause = False
def next_music(self) -> None:
""" Skip to the next song"""
if self.song_list.curselection()[0] == len(self.songs) - 1:
self.song_list.select_clear(0, END)
self.song_list.select_set(0)
self.current_song = self.songs[0]
self.play_music(False)
else:
self.song_list.select_clear(0, END)
self.song_list.select_set(self.songs.index(self.current_song) + 1)
self.current_song = self.songs[self.song_list.curselection()[0]]
self.play_music(False)
def prev_music(self) -> None:
""" Previous song function"""
if self.song_list.curselection()[0] == 0:
self.song_list.select_clear(0, END)
self.song_list.select_set(len(self.songs) - 1)
self.current_song = self.songs[len(self.songs) - 1]
self.play_music(False)
else:
self.song_list.select_clear(0, END)
self.song_list.select_set(self.songs.index(self.current_song) - 1)
self.current_song = self.songs[self.song_list.curselection()[0]]
self.play_music(False)
# print(self.song_list.curselection()[0])
self.current_song = self.songs[self.song_list.curselection()[0]]
self.play_music(False)
def back(self):
pygame.mixer.quit()
self.frame.destroy()
EmotionDetection.Emotion(self.master)
# if __name__ == "__main__":
# app = Tk()
# Window(app, "Kwame")
# app.mainloop()