import tkinter as tk from tkinter import filedialog import threading import pygame # Necesitas instalar pygame: pip install pygame class MusicPlayer: def __init__(self, parent): self.parent = parent self.is_playing = False # Inicializar el reproductor de música pygame.mixer.init() # Crear marco para el reproductor self.frame = tk.Frame(self.parent, bg="lightgreen", width=200, height=100) self.frame.pack(side="bottom", padx=10, pady=10, fill="both", expand=False) # Etiqueta de título self.title_label = tk.Label( self.frame, text="Reproductor de Música", font=("Arial", 12, "bold"), bg="lightgreen" ) self.title_label.pack(pady=5) # Botón para seleccionar archivo self.select_button = tk.Button( self.frame, text="Seleccionar Archivo", command=self.select_file, width=20 ) self.select_button.pack(pady=5) # Crear un marco para los botones de control self.controls_frame = tk.Frame(self.frame, bg="lightgreen") self.controls_frame.pack(pady=10) # Botones de control (centrados) self.play_button = tk.Button( self.controls_frame, text="▶ Reproducir", command=self.play_music, width=12 ) self.play_button.grid(row=0, column=0, padx=5) self.stop_button = tk.Button( self.controls_frame, text="■ Detener", command=self.stop_music, state="disabled", width=12 ) self.stop_button.grid(row=0, column=1, padx=5) def select_file(self): """Abrir el selector de archivos para elegir un archivo de música.""" self.music_file = filedialog.askopenfilename( filetypes=[("Archivos de audio", "*.mp3 *.wav"), ("Todos los archivos", "*.*")] ) if self.music_file: self.title_label.config(text=f"Archivo: {self.music_file.split('/')[-1]}") def play_music(self): """Iniciar la reproducción de música.""" if hasattr(self, "music_file"): self.is_playing = True self.play_button.config(state="disabled") self.stop_button.config(state="normal") threading.Thread(target=self._play_music_thread, daemon=True).start() def _play_music_thread(self): """Hilo que reproduce la música.""" pygame.mixer.music.load(self.music_file) pygame.mixer.music.play() while pygame.mixer.music.get_busy(): if not self.is_playing: pygame.mixer.music.stop() break def stop_music(self): """Detener la reproducción de música.""" self.is_playing = False self.play_button.config(state="normal") self.stop_button.config(state="disabled") pygame.mixer.music.stop()