import pygame as pg from tkinter import messagebox import os class MusicPlayer: def __init__(self, root_app): self.root_app = root_app self.current_file = None self.is_playing = False try: # Inicializar el mezclador de Pygame. # Frecuencia, tamaño de bits, canales, y tamaño del buffer (menor buffer = menor latencia) pg.mixer.init(frequency=44100, size=-16, channels=2, buffer=512) except pg.error as e: messagebox.showerror("Error de Audio", f"No se pudo inicializar Pygame Mixer. Asegúrate de tener hardware de audio: {e}") def load_and_play(self, filepath): """Carga un archivo de música y comienza la reproducción.""" if not os.path.exists(filepath): messagebox.showerror("Error de Archivo", "El archivo de música no se encuentra.") return try: # Detener cualquier reproducción actual pg.mixer.music.stop() # Cargar el nuevo archivo pg.mixer.music.load(filepath) self.current_file = filepath # Reproducir la música en bucle (-1) pg.mixer.music.play(-1) self.is_playing = True self.root_app.after(0, self.root_app.update_audio_status, f"Reproduciendo: {os.path.basename(filepath)}") return True except pg.error as e: messagebox.showerror("Error de Reproducción", f"No se pudo reproducir el archivo: {e}") self.is_playing = False return False def pause_music(self): """Pausa la música si está reproduciéndose.""" if self.is_playing and pg.mixer.music.get_busy(): pg.mixer.music.pause() self.is_playing = False self.root_app.after(0, self.root_app.update_audio_status, "Música: Pausada") return True return False def unpause_music(self): """Reanuda la música si está en pausa.""" if not self.is_playing and self.current_file: pg.mixer.music.unpause() self.is_playing = True self.root_app.after(0, self.root_app.update_audio_status, f"Reproduciendo: {os.path.basename(self.current_file)}") return True return False def stop_music(self): """Detiene completamente la música y libera el recurso.""" if pg.mixer.music.get_busy(): pg.mixer.music.stop() self.is_playing = False self.current_file = None self.root_app.after(0, self.root_app.update_audio_status, "Música: Detenida") return True return False # Fin de audio_player_logic.py