83 lines
2.9 KiB
Python
83 lines
2.9 KiB
Python
import os
|
|
import time
|
|
import threading
|
|
|
|
import pygame
|
|
from tkinter import Tk, Frame, Button, Label, StringVar
|
|
|
|
from app.widgets.abc import ThreadedTab
|
|
|
|
|
|
class MusicPlayerTab(ThreadedTab):
|
|
|
|
def __init__(self, root: Frame | Tk, **kwargs):
|
|
self.music_dir = "music"
|
|
self.music_files = []
|
|
self.current_index = 0
|
|
self.is_playing = False
|
|
self.current_song = StringVar()
|
|
super().__init__(root, **kwargs)
|
|
pygame.mixer.init()
|
|
|
|
def build(self):
|
|
# Create the main frame for the music player interface
|
|
self.player_frame = Frame(self)
|
|
self.player_frame.pack(fill="both", expand=True)
|
|
|
|
# Create the label to display the current song
|
|
self.song_label = Label(self.player_frame, textvariable=self.current_song, anchor="center", wraplength=200)
|
|
self.song_label.pack(padx=5, pady=5)
|
|
|
|
# Create the control buttons frame
|
|
self.controls_frame = Frame(self.player_frame)
|
|
self.controls_frame.pack(expand=True)
|
|
|
|
# Create the control buttons
|
|
self.prev_button = Button(self.controls_frame, text="<", command=self.previous_song)
|
|
self.prev_button.pack(side="left", padx=5)
|
|
|
|
self.play_pause_button = Button(self.controls_frame, text="|| / >", command=self.play_pause_music)
|
|
self.play_pause_button.pack(side="left", padx=5)
|
|
|
|
self.next_button = Button(self.controls_frame, text=">", command=self.next_song)
|
|
self.next_button.pack(side="left", padx=5)
|
|
|
|
self.load_music()
|
|
|
|
def load_music(self):
|
|
if not os.path.exists(self.music_dir):
|
|
os.makedirs(self.music_dir)
|
|
|
|
self.music_files = [f for f in os.listdir(self.music_dir) if f.endswith('.mp3')]
|
|
if self.music_files:
|
|
self.current_song.set(self.music_files[self.current_index])
|
|
|
|
def play_pause_music(self):
|
|
if self.is_playing:
|
|
pygame.mixer.music.pause()
|
|
self.is_playing = False
|
|
else:
|
|
if pygame.mixer.music.get_busy():
|
|
pygame.mixer.music.unpause()
|
|
else:
|
|
pygame.mixer.music.load(os.path.join(self.music_dir, self.music_files[self.current_index]))
|
|
pygame.mixer.music.play()
|
|
self.is_playing = True
|
|
|
|
def next_song(self):
|
|
self.current_index = (self.current_index + 1) % len(self.music_files)
|
|
self.current_song.set(self.music_files[self.current_index])
|
|
pygame.mixer.music.load(os.path.join(self.music_dir, self.music_files[self.current_index]))
|
|
pygame.mixer.music.play()
|
|
self.is_playing = True
|
|
|
|
def previous_song(self):
|
|
self.current_index = (self.current_index - 1) % len(self.music_files)
|
|
self.current_song.set(self.music_files[self.current_index])
|
|
pygame.mixer.music.load(os.path.join(self.music_dir, self.music_files[self.current_index]))
|
|
pygame.mixer.music.play()
|
|
self.is_playing = True
|
|
|
|
def task(self):
|
|
self.load_music()
|