0% found this document useful (0 votes)
7 views

Music Player - Python

Uploaded by

linren2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Music Player - Python

Uploaded by

linren2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Music Player

import tkinter as tk
from tkinter import filedialog, messagebox, ttk
import pygame
import os
import random

# Initialize Pygame mixer


pygame.mixer.init()

class MusicPlayer:
def __init__(self, root):
self.root = root
self.root.title("Music Player")
self.root.geometry("600x400")

self.playlist = []
self.current_index = 0
self.is_paused = False
self.is_shuffled = False
self.is_repeating = False

self.create_widgets()

def create_widgets(self):
self.playlist_box = tk.Listbox(self.root, selectmode=tk.SINGLE, width=60, height=15)
self.playlist_box.pack(pady=20)

controls_frame = tk.Frame(self.root)
controls_frame.pack()

self.play_button = tk.Button(controls_frame, text="Play", command=self.play_music)


self.play_button.grid(row=0, column=0, padx=10)

self.pause_button = tk.Button(controls_frame, text="Pause",


command=self.pause_music)
self.pause_button.grid(row=0, column=1, padx=10)

self.stop_button = tk.Button(controls_frame, text="Stop", command=self.stop_music)


self.stop_button.grid(row=0, column=2, padx=10)

self.next_button = tk.Button(controls_frame, text="Next", command=self.next_music)


self.next_button.grid(row=0, column=3, padx=10)

self.prev_button = tk.Button(controls_frame, text="Previous",


command=self.prev_music)
self.prev_button.grid(row=0, column=4, padx=10)
self.shuffle_button = tk.Button(controls_frame, text="Shuffle",
command=self.toggle_shuffle)
self.shuffle_button.grid(row=0, column=5, padx=10)

self.repeat_button = tk.Button(controls_frame, text="Repeat",


command=self.toggle_repeat)
self.repeat_button.grid(row=0, column=6, padx=10)

self.volume_slider = ttk.Scale(controls_frame, from_=0, to=1, orient=tk.HORIZONTAL,


command=self.set_volume)
self.volume_slider.set(0.5)
self.volume_slider.grid(row=1, columnspan=7, pady=20)

menu_bar = tk.Menu(self.root)
self.root.config(menu=menu_bar)

add_song_menu = tk.Menu(menu_bar, tearoff=0)


menu_bar.add_cascade(label="Add Songs", menu=add_song_menu)
add_song_menu.add_command(label="Add Songs to Playlist",
command=self.add_songs)

def add_songs(self):
songs = filedialog.askopenfilenames(title="Choose Songs", filetypes=(("MP3 Files",
"*.mp3"), ("All Files", "*.*")))
for song in songs:
song_name = os.path.basename(song)
self.playlist_box.insert(tk.END, song_name)
self.playlist.append(song)

def play_music(self):
if not self.playlist:
messagebox.showwarning("No Songs", "Please add songs to the playlist.")
return

if self.is_paused:
pygame.mixer.music.unpause()
self.is_paused = False
else:
pygame.mixer.music.load(self.playlist[self.current_index])
pygame.mixer.music.play()

def pause_music(self):
pygame.mixer.music.pause()
self.is_paused = True

def stop_music(self):
pygame.mixer.music.stop()
def next_music(self):
self.stop_music()
if self.is_shuffled:
self.current_index = random.randint(0, len(self.playlist) - 1)
else:
self.current_index = (self.current_index + 1) % len(self.playlist)

self.play_music()

def prev_music(self):
self.stop_music()
self.current_index = (self.current_index - 1) % len(self.playlist)
self.play_music()

def toggle_shuffle(self):
self.is_shuffled = not self.is_shuffled
status = "On" if self.is_shuffled else "Off"
messagebox.showinfo("Shuffle", f"Shuffle is now {status}")

def toggle_repeat(self):
self.is_repeating = not self.is_repeating
pygame.mixer.music.set_endevent(pygame.USEREVENT)
status = "On" if self.is_repeating else "Off"
messagebox.showinfo("Repeat", f"Repeat is now {status}")

def set_volume(self, val):


volume = float(val)
pygame.mixer.music.set_volume(volume)

def mainloop(self):
self.root.mainloop()

if __name__ == "__main__":
root = tk.Tk()
app = MusicPlayer(root)

# Event to handle repeat functionality


def check_repeat():
if app.is_repeating and not pygame.mixer.music.get_busy():
app.play_music()
root.after(1000, check_repeat)

check_repeat()
app.mainloop()

You might also like