Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
jear
/
Multimedia_Final_Practice
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
2661f5e5
authored
May 24, 2025
by
jear
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Initial commit
parents
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
270 additions
and
0 deletions
media player.py
media player.py
0 → 100644
View file @
2661f5e5
import
tkinter
as
tk
from
tkinter
import
filedialog
,
messagebox
from
PIL
import
Image
,
ImageTk
from
mutagen.mp3
import
MP3
from
mutagen.id3
import
ID3
import
pygame
import
os
import
random
# Initialize Pygame mixer
pygame
.
mixer
.
init
()
class
MediaPlayer
:
def
__init__
(
self
,
root
):
self
.
root
=
root
self
.
root
.
title
(
"🎵 Python Media Player 🎵"
)
self
.
root
.
geometry
(
"700x550"
)
self
.
root
.
configure
(
bg
=
"#2e2e2e"
)
self
.
playlist
=
[]
self
.
current_index
=
0
# UI Elements
self
.
listbox
=
tk
.
Listbox
(
root
,
bg
=
"#1c1c1c"
,
fg
=
"lime"
,
font
=
(
"Helvetica"
,
12
),
width
=
60
,
selectbackground
=
"#444"
)
self
.
listbox
.
pack
(
pady
=
20
)
control_frame
=
tk
.
Frame
(
root
,
bg
=
"#2e2e2e"
)
control_frame
.
pack
()
self
.
create_button
(
control_frame
,
"➕ Add Songs"
,
self
.
add_songs
,
0
)
self
.
create_button
(
control_frame
,
"📁 Add Folder"
,
self
.
add_folder
,
1
)
self
.
create_button
(
control_frame
,
"▶️ Play"
,
self
.
play_song
,
2
)
self
.
create_button
(
control_frame
,
"⏸️ Pause"
,
self
.
pause_song
,
3
)
self
.
create_button
(
control_frame
,
"⏹️ Stop"
,
self
.
stop_song
,
4
)
nav_frame
=
tk
.
Frame
(
root
,
bg
=
"#2e2e2e"
)
nav_frame
.
pack
(
pady
=
10
)
self
.
create_button
(
nav_frame
,
"⏮️ Previous"
,
self
.
play_previous
,
0
)
self
.
create_button
(
nav_frame
,
"⏭️ Next"
,
self
.
play_next
,
1
)
self
.
create_button
(
nav_frame
,
"🔀 Shuffle"
,
self
.
shuffle_playlist
,
2
)
self
.
meta_label
=
tk
.
Label
(
root
,
text
=
"Metadata will appear here"
,
wraplength
=
600
,
fg
=
"white"
,
bg
=
"#2e2e2e"
,
font
=
(
"Helvetica"
,
10
))
self
.
meta_label
.
pack
(
pady
=
10
)
self
.
cover_label
=
tk
.
Label
(
root
,
bg
=
"#2e2e2e"
)
self
.
cover_label
.
pack
()
def
create_button
(
self
,
frame
,
text
,
command
,
column
):
button
=
tk
.
Button
(
frame
,
text
=
text
,
command
=
command
,
bg
=
"#444"
,
fg
=
"white"
,
font
=
(
"Helvetica"
,
10
,
"bold"
),
relief
=
"flat"
,
activebackground
=
"#666"
,
activeforeground
=
"lime"
)
button
.
grid
(
row
=
0
,
column
=
column
,
padx
=
5
,
pady
=
5
)
def
add_songs
(
self
):
files
=
filedialog
.
askopenfilenames
(
filetypes
=
[(
"MP3 Files"
,
"*.mp3"
)])
for
f
in
files
:
self
.
playlist
.
append
(
f
)
self
.
listbox
.
insert
(
tk
.
END
,
os
.
path
.
basename
(
f
))
def
add_folder
(
self
):
folder
=
filedialog
.
askdirectory
()
if
folder
:
for
file
in
os
.
listdir
(
folder
):
if
file
.
lower
()
.
endswith
(
".mp3"
):
full_path
=
os
.
path
.
join
(
folder
,
file
)
self
.
playlist
.
append
(
full_path
)
self
.
listbox
.
insert
(
tk
.
END
,
os
.
path
.
basename
(
full_path
))
def
play_song
(
self
):
try
:
self
.
current_index
=
self
.
listbox
.
curselection
()[
0
]
except
IndexError
:
return
song_path
=
self
.
playlist
[
self
.
current_index
]
pygame
.
mixer
.
music
.
load
(
song_path
)
pygame
.
mixer
.
music
.
play
()
self
.
display_metadata
(
song_path
)
def
pause_song
(
self
):
pygame
.
mixer
.
music
.
pause
()
def
stop_song
(
self
):
pygame
.
mixer
.
music
.
stop
()
def
play_next
(
self
):
if
self
.
current_index
<
len
(
self
.
playlist
)
-
1
:
self
.
current_index
+=
1
self
.
listbox
.
select_clear
(
0
,
tk
.
END
)
self
.
listbox
.
select_set
(
self
.
current_index
)
self
.
play_song
()
def
play_previous
(
self
):
if
self
.
current_index
>
0
:
self
.
current_index
-=
1
self
.
listbox
.
select_clear
(
0
,
tk
.
END
)
self
.
listbox
.
select_set
(
self
.
current_index
)
self
.
play_song
()
def
shuffle_playlist
(
self
):
random
.
shuffle
(
self
.
playlist
)
self
.
listbox
.
delete
(
0
,
tk
.
END
)
for
song
in
self
.
playlist
:
self
.
listbox
.
insert
(
tk
.
END
,
os
.
path
.
basename
(
song
))
self
.
current_index
=
0
self
.
listbox
.
select_set
(
self
.
current_index
)
self
.
play_song
()
def
display_metadata
(
self
,
song_path
):
try
:
audio
=
MP3
(
song_path
,
ID3
=
ID3
)
title
=
str
(
audio
.
get
(
"TIT2"
,
"Unknown Title"
))
artist
=
str
(
audio
.
get
(
"TPE1"
,
"Unknown Artist"
))
meta_text
=
f
"🎵 Title: {title}
\n
🎤 Artist: {artist}"
self
.
meta_label
.
config
(
text
=
meta_text
)
except
Exception
as
e
:
self
.
meta_label
.
config
(
text
=
f
"Metadata not found. Error: {e}"
)
try
:
tags
=
ID3
(
song_path
)
for
tag
in
tags
.
values
():
if
tag
.
FrameID
==
'APIC'
:
# Album art
img
=
Image
.
open
(
tag
.
data
)
img
=
img
.
resize
((
120
,
120
))
photo
=
ImageTk
.
PhotoImage
(
img
)
self
.
cover_label
.
config
(
image
=
photo
)
self
.
cover_label
.
image
=
photo
return
except
:
self
.
cover_label
.
config
(
image
=
''
,
text
=
'No Cover Found'
)
if
__name__
==
'__main__'
:
root
=
tk
.
Tk
()
app
=
MediaPlayer
(
root
)
root
.
mainloop
()
import
tkinter
as
tk
from
tkinter
import
filedialog
,
messagebox
from
PIL
import
Image
,
ImageTk
from
mutagen.mp3
import
MP3
from
mutagen.id3
import
ID3
import
pygame
import
os
import
random
# Initialize Pygame mixer
pygame
.
mixer
.
init
()
class
MediaPlayer
:
def
__init__
(
self
,
root
):
self
.
root
=
root
self
.
root
.
title
(
"🎵 Python Media Player 🎵"
)
self
.
root
.
geometry
(
"700x550"
)
self
.
root
.
configure
(
bg
=
"#2e2e2e"
)
self
.
playlist
=
[]
self
.
current_index
=
0
# UI Elements
self
.
listbox
=
tk
.
Listbox
(
root
,
bg
=
"#1c1c1c"
,
fg
=
"lime"
,
font
=
(
"Helvetica"
,
12
),
width
=
60
,
selectbackground
=
"#444"
)
self
.
listbox
.
pack
(
pady
=
20
)
control_frame
=
tk
.
Frame
(
root
,
bg
=
"#2e2e2e"
)
control_frame
.
pack
()
self
.
create_button
(
control_frame
,
"➕ Add Songs"
,
self
.
add_songs
,
0
)
self
.
create_button
(
control_frame
,
"📁 Add Folder"
,
self
.
add_folder
,
1
)
self
.
create_button
(
control_frame
,
"▶️ Play"
,
self
.
play_song
,
2
)
self
.
create_button
(
control_frame
,
"⏸️ Pause"
,
self
.
pause_song
,
3
)
self
.
create_button
(
control_frame
,
"⏹️ Stop"
,
self
.
stop_song
,
4
)
nav_frame
=
tk
.
Frame
(
root
,
bg
=
"#2e2e2e"
)
nav_frame
.
pack
(
pady
=
10
)
self
.
create_button
(
nav_frame
,
"⏮️ Previous"
,
self
.
play_previous
,
0
)
self
.
create_button
(
nav_frame
,
"⏭️ Next"
,
self
.
play_next
,
1
)
self
.
create_button
(
nav_frame
,
"🔀 Shuffle"
,
self
.
shuffle_playlist
,
2
)
self
.
meta_label
=
tk
.
Label
(
root
,
text
=
"Metadata will appear here"
,
wraplength
=
600
,
fg
=
"white"
,
bg
=
"#2e2e2e"
,
font
=
(
"Helvetica"
,
10
))
self
.
meta_label
.
pack
(
pady
=
10
)
self
.
cover_label
=
tk
.
Label
(
root
,
bg
=
"#2e2e2e"
)
self
.
cover_label
.
pack
()
def
create_button
(
self
,
frame
,
text
,
command
,
column
):
button
=
tk
.
Button
(
frame
,
text
=
text
,
command
=
command
,
bg
=
"#444"
,
fg
=
"white"
,
font
=
(
"Helvetica"
,
10
,
"bold"
),
relief
=
"flat"
,
activebackground
=
"#666"
,
activeforeground
=
"lime"
)
button
.
grid
(
row
=
0
,
column
=
column
,
padx
=
5
,
pady
=
5
)
def
add_songs
(
self
):
files
=
filedialog
.
askopenfilenames
(
filetypes
=
[(
"MP3 Files"
,
"*.mp3"
)])
for
f
in
files
:
self
.
playlist
.
append
(
f
)
self
.
listbox
.
insert
(
tk
.
END
,
os
.
path
.
basename
(
f
))
def
add_folder
(
self
):
folder
=
filedialog
.
askdirectory
()
if
folder
:
for
file
in
os
.
listdir
(
folder
):
if
file
.
lower
()
.
endswith
(
".mp3"
):
full_path
=
os
.
path
.
join
(
folder
,
file
)
self
.
playlist
.
append
(
full_path
)
self
.
listbox
.
insert
(
tk
.
END
,
os
.
path
.
basename
(
full_path
))
def
play_song
(
self
):
try
:
self
.
current_index
=
self
.
listbox
.
curselection
()[
0
]
except
IndexError
:
return
song_path
=
self
.
playlist
[
self
.
current_index
]
pygame
.
mixer
.
music
.
load
(
song_path
)
pygame
.
mixer
.
music
.
play
()
self
.
display_metadata
(
song_path
)
def
pause_song
(
self
):
pygame
.
mixer
.
music
.
pause
()
def
stop_song
(
self
):
pygame
.
mixer
.
music
.
stop
()
def
play_next
(
self
):
if
self
.
current_index
<
len
(
self
.
playlist
)
-
1
:
self
.
current_index
+=
1
self
.
listbox
.
select_clear
(
0
,
tk
.
END
)
self
.
listbox
.
select_set
(
self
.
current_index
)
self
.
play_song
()
def
play_previous
(
self
):
if
self
.
current_index
>
0
:
self
.
current_index
-=
1
self
.
listbox
.
select_clear
(
0
,
tk
.
END
)
self
.
listbox
.
select_set
(
self
.
current_index
)
self
.
play_song
()
def
shuffle_playlist
(
self
):
random
.
shuffle
(
self
.
playlist
)
self
.
listbox
.
delete
(
0
,
tk
.
END
)
for
song
in
self
.
playlist
:
self
.
listbox
.
insert
(
tk
.
END
,
os
.
path
.
basename
(
song
))
self
.
current_index
=
0
self
.
listbox
.
select_set
(
self
.
current_index
)
self
.
play_song
()
def
display_metadata
(
self
,
song_path
):
try
:
audio
=
MP3
(
song_path
,
ID3
=
ID3
)
title
=
str
(
audio
.
get
(
"TIT2"
,
"Unknown Title"
))
artist
=
str
(
audio
.
get
(
"TPE1"
,
"Unknown Artist"
))
meta_text
=
f
"🎵 Title: {title}
\n
🎤 Artist: {artist}"
self
.
meta_label
.
config
(
text
=
meta_text
)
except
Exception
as
e
:
self
.
meta_label
.
config
(
text
=
f
"Metadata not found. Error: {e}"
)
try
:
tags
=
ID3
(
song_path
)
for
tag
in
tags
.
values
():
if
tag
.
FrameID
==
'APIC'
:
# Album art
img
=
Image
.
open
(
tag
.
data
)
img
=
img
.
resize
((
120
,
120
))
photo
=
ImageTk
.
PhotoImage
(
img
)
self
.
cover_label
.
config
(
image
=
photo
)
self
.
cover_label
.
image
=
photo
return
except
:
self
.
cover_label
.
config
(
image
=
''
,
text
=
'No Cover Found'
)
if
__name__
==
'__main__'
:
root
=
tk
.
Tk
()
app
=
MediaPlayer
(
root
)
root
.
mainloop
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment