import pygame
import mutagen
pygame.init()
from mutagen.easyid3 import EasyID3
from tkinter.filedialog import * # module contains convenience class and func. for creating simple modal dialogs to get a value from the user
from tkinter import * # Python interface to the Tcl/Tk GUI toolkit
global interface
global song_list
global list_index
global song_name
global pausing
global song_end
class FrameApp(Frame):
def __init__ (self, master):
super(FrameApp, self).__init__(master)
global interface, song_name, song_list, pausing, list_index, song_end
self.grid()
b1 = Button(self, text="PLAY SONG", command=play_song, bg='AntiqueWhite1', width=40)
b1.grid(row=2, column=0)
b2 = Button(self, text="PREVIOUS SONG", command=previous_song, bg='AntiqueWhite1', width=40)
b2.grid(row=4, column=0)
b3 = Button(self, text="PAUSE/UNPAUSE", command=pause_unpause, bg='AntiqueWhite1', width=40)
b3.grid(row=3, column=0)
b4 = Button(self, text="NEXT SONG", command=next_song, bg='AntiqueWhite1', width=40)
b4.grid(row=5, column=0)
b5 = Button(self, text="ADD TO LIST", command=add_to_list, bg='AntiqueWhite1', width=40)
b5.grid(row=1, column=0)
song_name = Label(self, fg='Black', font=('Helvetica 12 bold italic', 10), bg='ivory2')
song_name.grid(row=6, column=0)
interface = Text(self, wrap=WORD, width=60)
interface.grid(row=8, column=0)
song_list = list()
pausing = False
list_index = 0
song_end = pygame.USEREVENT + 1
def add_to_list():
global list_index, song_list, song_end, pausing, song_name, interface
try:
directory = askopenfilenames()
for song_directory in directory:
print(song_directory)
song_list.append(song_directory)
interface.delete(0.0, END)
for key, item in enumerate(song_list):
song = EasyID3(item)
song_data = (str(key + 1) + ' : ' + song['title'][0] + ' - '
+ song['artist'][0])
interface.insert(END, song_data + '\n')
except:
pass
def play_song():
global list_index, song_list, song_end, pausing, song_name, interface
try:
directory = song_list [list_index]
pygame.mixer.music.load(directory)
pygame.mixer.music.play(1, 0.0)
pygame.mixer.music.set_endevent(song_end)
pausing = False
song_name['text'] = song_data()
except:
pass
def pause_unpause():
global list_index, song_list, song_end, pausing, song_name, interface
try:
if pausing:
pygame.mixer.music.unpause()
pausing = False
elif not pausing:
pygame.mixer.music.pause()
pausing = True
except:
pass
def previous_song():
global list_index, song_list, song_end, pausing, song_name, interface
try:
list_index = get_previous_song()
play_song()
except:
pass
def next_song():
global list_index, song_list, song_end, pausing, song_name, interface
try:
list_index = get_next_song()
play_song()
except:
pass
def song_data():
global list_index, song_list, song_end, pausing, song_name, interface
try:
song = EasyID3(song_list[list_index])
song_data = "Now playing: Nr:" + str(list_index + 1) + " " + \
str(song['title']) + " - " + str(song['artist'])
return song_data
except:
pass
def check_music():
global list_index, song_list, song_end, pausing, song_name, interface
try:
for event in pygame.event.get():
if event.type == song_end:
next_song()
except:
pass
def get_next_song():
global list_index, song_list, song_end, pausing, song_name, interface
try:
if list_index + 2 <= len(song_list):
return list_index + 1
else:
return 0
except:
pass
def get_previous_song():
global list_index, song_list, song_end, pausing, song_name, interface
try:
if list_index - 1 >= 0:
return list_index - 1
else:
return len(song_list) - 1
except:
pass
window = Tk()
window.geometry("500x500")
window.title("MP3 Music Player")
app = FrameApp(window)
while True:
# runs mainloop of program
check_music()
app.update()