elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Tutorial básico de Quickjs


+  Foro de elhacker.net
|-+  Programación
| |-+  Scripting
| | |-+  Reproductor cli en python
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Reproductor cli en python  (Leído 1,382 veces)
@synthesize
Wiki

Desconectado Desconectado

Mensajes: 640


Another Brick in the Wall


Ver Perfil WWW
Reproductor cli en python
« en: 10 Diciembre 2011, 17:51 pm »

Hola a tod@s:

Estoy haciendo con un conocido un reproductor de música en línea de comandos muy sencillo: Recoge un diccionario con todas las canciones y las reproduce aleatoriamente. Pero tenemos un problema, sólo reproduce la 1ª canción: no pasa a la siguiente ¿Alguien ve el fallo?

Código
  1.  
  2. #!/usr/bin/env python2
  3. #-*- coding: UTF-8 -*-
  4.  
  5. import os, gobject
  6. import pygst
  7. pygst.require("0.10")
  8. import gst
  9.  
  10. from os.path import join, getsize
  11. from mimetypes import guess_type
  12. from random import shuffle
  13.  
  14. gobject.threads_init()
  15.  
  16. class AllFromDirectory():
  17.  
  18. def __init__(self, directory):
  19. self.directory = directory
  20. self.biblio = dict()
  21. self.autoid = 0
  22.  
  23. def get(self):
  24. for root, dirs, files in os.walk(self.directory):
  25. for self.filen in files:
  26. self.mime = guess_type(self.filen)
  27. self.auxmime = str(self.mime[0])
  28. self.endmime = self.auxmime.split(os.sep)
  29. if self.endmime[0] == "audio":
  30. self.biblio[self.autoid] = root+os.sep+self.filen
  31. self.autoid = self.autoid + 1
  32.  
  33. return self.biblio
  34.  
  35. def getLen(self):
  36. return len(self.biblio)
  37.  
  38.  
  39. class Player():
  40. def __init__(self):
  41.  
  42. # Obtenemos la lista de archivos
  43. x = AllFromDirectory(directory)
  44. self.biblio = x.get()
  45. self.total_tracks = x.getLen()
  46. self.numeros = range(0, int(self.total_tracks))
  47. shuffle(self.numeros)
  48. self.n = 0
  49.  
  50. self.create_pipeline()
  51.  
  52. def create_pipeline(self):
  53.  
  54. n = self.numeros[0]
  55.  
  56. self.song = self.biblio[n].split(os.sep)
  57. print 'Reproduciendo: '+self.song[-1]
  58.  
  59. cdsrc = 'filesrc location="%s" name=file ! decodebin ! volume name=volume ! alsasink' % (self.biblio[n])
  60. self.pipeline = gst.parse_launch(cdsrc)
  61.  
  62. bus = self.pipeline.get_bus()
  63.  
  64. bus.add_signal_watch()
  65.  
  66. bus.connect("message::tag", self.bus_message_tag)
  67. bus.connect("message::error", self.bus_message_error)
  68. bus.connect("message::eos", self.nexts)
  69.  
  70. self.pipeline.set_state(gst.STATE_PLAYING)
  71.  
  72. self.loop = gobject.MainLoop()
  73. self.loop.run()
  74.  
  75. def bus_message_error(self, bus, message):
  76. e, d = message.parse_error()
  77. print "ERROR:", e
  78. #exit(1)
  79.  
  80. def bus_message_tag(self, bus, message):
  81.  
  82. """Esta es la función encargada de recoger los datos del bus de Gstreamer, principalmente los tags de los ficheros de audio"""
  83. self.tags = ''
  84. #we received a tag message
  85. taglist = message.parse_tag()
  86. #put the keys in the dictionary
  87. for key in taglist.keys():
  88. try:
  89. self.file_tags[key] = taglist[key]
  90. except:
  91. return False
  92.  
  93. try:
  94. self.tags += self.file_tags['title'] + " "
  95. except:
  96. self.tags += "Título desconocido."
  97.  
  98. try:
  99. self.tags += "de: "+self.file_tags['artist']+"\n"
  100. except:
  101. self.tags += "Artista desconocido."
  102.  
  103. self.song = self.tags.split(os.sep)
  104. print 'Reproduciendo', self.song[-1]
  105.  
  106. def nexts(self, w, *args):
  107.  
  108. if self.n < self.total_tracks:
  109. self.n += 1
  110. else:
  111. self.n = 0
  112.  
  113. n = self.numeros[self.n]
  114. self.song = self.biblio[n].split(os.sep)
  115. print 'Reproduciendo: '+self.song[-1]
  116.  
  117. self.loop.quit()
  118. self.pipeline.set_state(gst.STATE_NULL)
  119. self.pipeline.get_by_name('file').set_property('location', self.biblio[n])
  120. self.pipeline.set_state(gst.STATE_PLAYING)
  121. self.loop.run()
  122.  
  123. directory = '/media/HD/Música'
  124. mega = list()
  125.  
  126. p = Player()
  127.  
  128.  


« Última modificación: 10 Diciembre 2011, 18:10 pm por Daemon Freedom » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
[Python] Curso de Python con Interfaces graficas TK « 1 2 »
Scripting
Erik# 18 38,651 Último mensaje 12 Septiembre 2010, 02:27 am
por Dreykon
[Python] Sockets en Python [+Ejemplos y Ejercicios]
Scripting
Erik# 4 14,830 Último mensaje 4 Abril 2013, 16:43 pm
por SelTzeR
(Python)Existen ventanas de entrada y salida de datos en python « 1 2 »
Scripting
tonilogar 11 15,623 Último mensaje 29 Noviembre 2009, 00:49 am
por tonilogar
[Python] Abrir una página web con comandos de Python. « 1 2 3 4 »
Scripting
CaronteGold 31 39,714 Último mensaje 29 Mayo 2015, 15:02 pm
por antkk
[python]VideoTraining Aprende A Programar En Python desde 0 « 1 2 3 »
Scripting
juh 22 17,504 Último mensaje 16 Octubre 2010, 13:12 pm
por Pere Navarro
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines