Advertisement
teslariu

audio player

Sep 14th, 2022
1,077
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.97 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. #  player.py
  5. #  
  6. #  Copyright 2022 Usuario <Usuario@DESKTOP-SBRCMHE>
  7. #  
  8. #  This program is free software; you can redistribute it and/or modify
  9. #  it under the terms of the GNU General Public License as published by
  10. #  the Free Software Foundation; either version 2 of the License, or
  11. #  (at your option) any later version.
  12. #  
  13. #  This program is distributed in the hope that it will be useful,
  14. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. #  GNU General Public License for more details.
  17. #  
  18. #  You should have received a copy of the GNU General Public License
  19. #  along with this program; if not, write to the Free Software
  20. #  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  21. #  MA 02110-1301, USA.
  22. #  
  23. import os
  24. import pygame
  25. import sys
  26.  
  27. from tkinter import *
  28. from tkinter import filedialog as fd
  29.  
  30.  
  31. class MusicPlayer():
  32.     def __init__(self,root):
  33.         self.root = root
  34.        
  35.         # Título
  36.         self.root.title("Reproductor ACME")
  37.        
  38.         # Dimensiones y posicion
  39.         self.root.geometry("1000x200")
  40.         self.root.resizable(True,True)
  41.         self.root.minsize(950,170)
  42.        
  43.         # Inicializamos Pygame
  44.         pygame.init()
  45.        
  46.         # Inicializo el mixer de Pygame
  47.         pygame.mixer.init()
  48.        
  49.         # Variable que almacena la canción a reproducir
  50.         self.pista = StringVar()
  51.        
  52.         # Variable que almacena el estado del reproductor
  53.         self.estado = StringVar()
  54.         self.estado.set("HOLA")
  55.        
  56.         # Creo el frame de las pistas con las etiquetas
  57.         # de las canciones y del estado del reproductor
  58.         frame_pistas = LabelFrame(
  59.                 self.root,
  60.                 text = "CANCION",
  61.                 font = ("times new roman", 15, "bold"),
  62.                 bg = "Gray",
  63.                 fg = "LightSteelBlue1",
  64.                 bd = 5,
  65.                 relief = GROOVE
  66.         )
  67.         frame_pistas.grid(row=0, column=0, sticky="nsew")
  68.        
  69.         # Insertamos la etiqueta con la pista de la cancion
  70.         cancion = Label(
  71.                 frame_pistas,
  72.                 textvariable = self.pista,
  73.                 width = 50,
  74.                 height = 2,
  75.                 font = ("times new roman", 12, "bold"),
  76.                 bg = "gold",
  77.                 fg = "dodger blue",
  78.         )
  79.         cancion.grid(row=0, column=0, padx=10, pady=5)
  80.  
  81.         # Insertamos la etiqueta con el estado del reproductor
  82.         frame_estado = Label(
  83.                 frame_pistas,
  84.                 textvariable = self.estado,
  85.                 width = 15,
  86.                 height = 2,
  87.                 font = ("times new roman", 14, "bold"),
  88.                 bg = "gold",
  89.                 fg = "dodger blue",
  90.         )
  91.         frame_estado.grid(row=0, column=1, padx=10, pady=5)
  92.        
  93.        
  94.         # Creo el frame para contener los botones
  95.         frame_boton = LabelFrame(
  96.                 self.root,
  97.                 text = "Panel de Control",
  98.                 font = ("times new roman", 15, "bold"),
  99.                 bg = "Gray",
  100.                 fg = "LightSteelBlue1",
  101.                 bd = 5,
  102.                 relief = GROOVE
  103.         )
  104.         frame_boton.grid(row=1, column=0, sticky="nsew")
  105.                
  106.        
  107.         # boton play
  108.         boton = Button(
  109.                 frame_boton,
  110.                 text = "\u25B6",
  111.                 command = self.playsong,
  112.                 width = 5,
  113.                 height = 1,
  114.                 font = ("times new roman", 16, "bold"),
  115.                 fg = "white",
  116.                 bg = "LightSteelBlue3",
  117.         )
  118.         boton.grid(row=0, column=0, padx=10, pady=5)
  119.  
  120.         # boton pausa
  121.         boton = Button(
  122.                 frame_boton,
  123.                 text = "\u23F8",
  124.                 command = self.pausesong,
  125.                 width = 5,
  126.                 height = 1,
  127.                 font = ("times new roman", 16, "bold"),
  128.                 fg = "white",
  129.                 bg = "LightSteelBlue3",
  130.         )
  131.         boton.grid(row=0, column=1, padx=10, pady=5)
  132.  
  133.  
  134.         # boton stop
  135.         boton = Button(
  136.                 frame_boton,
  137.                 text = "\u23F9",
  138.                 command = self.stopsong,
  139.                 width = 5,
  140.                 height = 1,
  141.                 font = ("times new roman", 16, "bold"),
  142.                 fg = "white",
  143.                 bg = "LightSteelBlue3",
  144.         )
  145.         boton.grid(row=0, column=2, padx=10, pady=5)
  146.        
  147.        
  148.         # boton para cargar lista de canciones
  149.         boton = Button(
  150.                 frame_boton,
  151.                 text = "Lista de Canciones",
  152.                 command = self.cargar,
  153.                 width = 20,
  154.                 height = 1,
  155.                 font = ("times new roman", 16, "bold"),
  156.                 fg = "white",
  157.                 bg = "LightSteelBlue3",
  158.         )
  159.         boton.grid(row=0, column=3, padx=10, pady=5)
  160.        
  161.        
  162.         # creo el frame para mostrar la lista de canciones
  163.         frame_cancion = LabelFrame(
  164.                 self.root,
  165.                 text = "Lista de canciones",
  166.                 font = ("times new roman", 15, "bold"),
  167.                 bg = "gray",
  168.                 fg = "white",
  169.                 bd = 5,
  170.                 relief = GROOVE
  171.         )
  172.         frame_cancion.grid(row=0, column=1, rowspan=2, sticky="nsew")
  173.        
  174.                
  175.        
  176.         # Configuro el tamaño de cada frame al redimensionar
  177.         self.root.columnconfigure(1, weight=5)
  178.         self.root.rowconfigure(0, weight=1)
  179.         self.root.rowconfigure(1, weight=1)
  180.        
  181.         # insertamos las barras de scroll
  182.         scroll_y = Scrollbar(frame_cancion, orient=VERTICAL)
  183.         scroll_x = Scrollbar(frame_cancion, orient=HORIZONTAL)
  184.        
  185.         # insertamos una listbox con las canciones
  186.         self.playlist = Listbox(
  187.                 frame_cancion,
  188.                 yscrollcommand = scroll_y.set,
  189.                 xscrollcommand = scroll_x.set,
  190.                 selectbackground = "blue",
  191.                 selectmode=SINGLE,
  192.                 font = ("times new roman", 12, "bold"),
  193.                 bg = "gray",
  194.                 fg = "white",
  195.                 bd = 5,
  196.                 relief = GROOVE
  197.         )
  198.        
  199.         # aplicamos las scrollbars con listbox
  200.         scroll_y.pack(side=RIGHT, fill=Y)
  201.         scroll_y.config(command=self.playlist.yview)
  202.         scroll_x.pack(side=BOTTOM, fill=X)
  203.         scroll_x.config(command=self.playlist.xview)
  204.         self.playlist.pack(expand=True, fill=BOTH)
  205.        
  206.        
  207.  
  208.     def playsong(self):
  209.         # reproduzco la canción
  210.         if self.estado.get() == "Pausado":
  211.             self.estado.set("Reproduciendo")
  212.             pygame.mixer.music.unpause()
  213.            
  214.         else:
  215.             # muestro la canción elegida
  216.             titulo = self.playlist.get(ACTIVE)
  217.             self.pista.set(os.path.basename(titulo))
  218.             self.estado.set("Reproduciendo")
  219.             # cargo la canción
  220.             pygame.mixer.music.load(self.playlist.get(ACTIVE))
  221.             pygame.mixer.music.play()
  222.  
  223.  
  224.     def pausesong(self):
  225.         self.estado.set("Pausado")
  226.         pygame.mixer.music.pause()
  227.        
  228.      
  229.        
  230.     def stopsong(self):
  231.         self.estado.set("Parado")
  232.         pygame.mixer.music.stop()
  233.        
  234.        
  235.     def cargar(self):
  236.         # Elijo el directorio donde cargar las canciones
  237.         ruta = fd.askdirectory()
  238.         self.playlist.delete(0,END)
  239.         # Insertar las canciones dentro de la lista
  240.         for carpeta_actual, lista_carpetas, lista_archivos in os.walk(ruta):
  241.             for archivo in lista_archivos:
  242.                 cancion = os.path.join(carpeta_actual,archivo)
  243.                 self.playlist.insert(END,cancion)
  244.  
  245.  
  246. if __name__ == '__main__':
  247.     root = Tk()
  248.     reproductor = MusicPlayer(root)
  249.     root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement