Skip to content

Commit 0333c51

Browse files
committed
🎨 Update
0 parents  commit 0333c51

File tree

5 files changed

+210
-0
lines changed

5 files changed

+210
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
env/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Eliaz Bobadilla
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# :snake: Python projects for Intermediates :snake:
2+
3+
<p>
4+
<a href="https://github.com/UltiRequiem/python-projects-for-intermediates/blob/main/LICENSE"><img alt="License: MIT" src="https://black.readthedocs.io/en/stable/_static/license.svg"></a>
5+
<a href="https://github.com/UltiRequiem/
6+
python-projects-for-intermediates"><img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
7+
<a href="https://github.com/UltiRequiem/
8+
python-projects-for-intermediates"><img alt="Code style: black" src="https://img.shields.io/tokei/lines/github.com/UltiRequiem/python-projects-for-intermediates?color=blue&label=Total%20Lines"></a>
9+
</p>
10+
11+
All Proyects are taken from [this Twitter thread](https://twitter.com/swastika0015/status/1403308100115861508)
12+
of [@swastika0015](https://twitter.com/swastika0015).
13+
14+
You also should check [UltiRequiem/python-projects-for-beginners](https://github.com/UltiRequiem/python-projects-for-beginners).
15+
16+
## List of Projects:
17+
18+
- [MP3 Player GUI](./mp3-player-gui/)

mp3-player-gui/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# MP3 Player using Tkinter and Mutagen
2+
3+
[Link](https://blog.devgenius.io/mp3-player-using-tkinter-and-mutagen-in-python-6fec027aeced).

mp3-player-gui/main.py

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import tkinter
2+
from mutagen.easyid3 import EasyID3
3+
import pygame
4+
from tkinter.filedialog import askopenfilenames
5+
6+
7+
class FrameApp(tkinter.Frame):
8+
def __init__(self, master):
9+
super(FrameApp, self).__init__(master)
10+
11+
self.grid()
12+
self.paused = False
13+
self.playlist = list()
14+
self.actual_song = 0
15+
16+
self.b1 = tkinter.Button(
17+
self,
18+
text="PLAY SONG",
19+
command=self.play_music,
20+
bg="AntiqueWhite1",
21+
width=40,
22+
)
23+
self.b1.grid(row=2, column=0)
24+
25+
self.b2 = tkinter.Button(
26+
self,
27+
text="PREVIOUS SONG",
28+
command=self.previous_song,
29+
bg="AntiqueWhite1",
30+
width=40,
31+
)
32+
self.b2.grid(row=4, column=0)
33+
34+
self.b3 = tkinter.Button(
35+
self,
36+
text="PAUSE/UNPAUSE",
37+
command=self.toggle,
38+
bg="AntiqueWhite1",
39+
width=40,
40+
)
41+
self.b3.grid(row=3, column=0)
42+
43+
self.b4 = tkinter.Button(
44+
self, text="NEXT SONG", command=self.next_song, bg="AntiqueWhite1", width=40
45+
)
46+
self.b4.grid(row=5, column=0)
47+
48+
self.b5 = tkinter.Button(
49+
self,
50+
text="ADD TO PLAYLIST",
51+
command=self.add_to_list,
52+
bg="AntiqueWhite1",
53+
width=40,
54+
)
55+
self.b5.grid(row=1, column=0)
56+
57+
self.label1 = tkinter.Label(
58+
self, fg="Black", font=("Helvetica 12 bold italic", 10), bg="ivory2"
59+
)
60+
self.label1.grid(row=6, column=0)
61+
62+
self.output = tkinter.Text(self, wrap=tkinter.WORD, width=60)
63+
self.output.grid(row=8, column=0)
64+
65+
self.SONG_END = pygame.USEREVENT + 1
66+
67+
def add_to_list(self) -> None:
68+
"""
69+
Opens window to browse data on disk and adds selected songs to play list
70+
"""
71+
directory = askopenfilenames()
72+
for song_dir in directory:
73+
print(song_dir)
74+
self.playlist.append(song_dir)
75+
self.output.delete(0.0, tkinter.END)
76+
77+
for key, item in enumerate(self.playlist):
78+
song = EasyID3(item)
79+
song_data = (
80+
str(key + 1) + " : " + song["title"][0] + " - " + song["artist"][0]
81+
)
82+
self.output.insert(tkinter.END, song_data + "\n")
83+
84+
def song_data(self) -> str:
85+
"""
86+
Makes string of current playing song data over the text box
87+
"""
88+
song = EasyID3(self.playlist[self.actual_song])
89+
return f"Now playing: {self.actual_song + 1}{str(song['title'])}-{str(song['artist'])}"
90+
91+
def play_music(self) -> None:
92+
"""
93+
Loads current song, plays it, sets event on song finish
94+
"""
95+
directory = self.playlist[self.actual_song]
96+
pygame.mixer.music.load(directory)
97+
pygame.mixer.music.play(1, 0.0)
98+
pygame.mixer.music.set_endevent(self.SONG_END)
99+
self.paused = False
100+
self.label1["text"] = self.song_data()
101+
102+
def check_music(self) -> None:
103+
"""
104+
Listens to END_MUSIC event and triggers next song to play if current
105+
song has finished
106+
"""
107+
for event in pygame.event.get():
108+
if event.type == self.SONG_END:
109+
self.next_song()
110+
111+
def toggle(self) -> None:
112+
"""
113+
Toggles current song
114+
"""
115+
if self.paused:
116+
pygame.mixer.music.unpause()
117+
self.paused = False
118+
elif not self.paused:
119+
pygame.mixer.music.pause()
120+
self.paused = True
121+
122+
def get_next_song(self) -> int:
123+
"""
124+
Gets next song number on playlist
125+
"""
126+
if self.actual_song + 2 <= len(self.playlist):
127+
return self.actual_song + 1
128+
else:
129+
return 0
130+
131+
def next_song(self) -> None:
132+
"""
133+
Plays next song
134+
"""
135+
self.actual_song = self.get_next_song()
136+
self.play_music()
137+
138+
def get_previous_song(self) -> int:
139+
"""
140+
Gets previous song number on playlist and returns it
141+
"""
142+
if self.actual_song - 1 >= 0:
143+
return self.actual_song - 1
144+
else:
145+
return len(self.playlist) - 1
146+
147+
def previous_song(self) -> None:
148+
"""
149+
Plays prevoius song
150+
"""
151+
self.actual_song = self.get_previous_song()
152+
self.play_music()
153+
154+
155+
pygame.init()
156+
window = tkinter.Tk()
157+
158+
window.geometry("500x500")
159+
window.title("MP3 Music Player")
160+
161+
162+
app = FrameApp(window)
163+
164+
if __name__ == "__main__":
165+
while True:
166+
app.check_music()
167+
app.update()

0 commit comments

Comments
 (0)