Sunday, June 14, 2015

REPRODUCTOR DE AUDIO WAV,MP3






Parece un poco largo el código, pero en realidad es corto ya que la mayoría es para habilitar o deshabilitar los controles de Play, Pause y Stop..
Para el ejemplo colocar los siguientes controles en un form:
  • 4 CommandButton: Command1 (Play) , Command2(stop) , Command3 (Pause) y Command4 (Abrir archivo)
  • Un Commondialog1
  • Un Label1: Para mostrar el Path


  1. Option Explicit  
  2. 'Función Api GetShortPathName para obtener _  
  3. los paths de los archivos en formato corto  
  4. Private Declare Function GetShortPathName _  
  5.     Lib "kernel32" _  
  6.     Alias "GetShortPathNameA" ( _  
  7.         ByVal lpszLongPath As String, _  
  8.         ByVal lpszShortPath As String, _  
  9.         ByVal lBuffer As LongAs Long  
  10.   
  11. 'Función Api mciExecute para reproducir los archivos de música  
  12. Private Declare Function mciExecute _  
  13.     Lib "winmm.dll" ( _  
  14.         ByVal lpstrCommand As StringAs Long  
  15. Dim ret As Long, path As String  
  16.   
  17. 'Le pasamos el comando Play  
  18. Private Sub Command1_Click()  
  19.     ejecutar ("Play ")  
  20.     Habilitar "Play"  
  21. End Sub  
  22.   
  23. Private Sub Command2_Click()  
  24.     'Le pasamos el comando Stop  
  25.     ejecutar ("Stop ")  
  26.     Habilitar "Stop"  
  27. End Sub  
  28.   
  29. 'Le pasamos el comando Pause  
  30. Private Sub Command3_Click()  
  31.     ejecutar ("Pause ")  
  32.     Habilitar "Pause"  
  33. End Sub  
  34.   
  35. 'Le pasamos el comando Close a MciExecute para cerrar el dispositivo  
  36. Private Sub Form_Unload(Cancel As Integer)  
  37.     mciExecute "Close All"  
  38. End Sub  
  39.   
  40. 'Botón para abrir seleccionar los archivos de audio  
  41. Private Sub Command4_Click()  
  42.     With CommonDialog1  
  43.         .Filter = "Archivos Wav|*.wav|Archivos Mp3|*.mp3|Archivos MIDI|*.mid"  
  44.         .ShowOpen  
  45.         If .FileName = "" Then  
  46.             Habilitar "Iniciar"  
  47.             Exit Sub  
  48.         Else  
  49.             'Le pasamos a la sub que obtiene con _  
  50.             el Api GetShortPathName el nombre corto del archivo  
  51.             PathCorto .FileName  
  52.             Label1 = .FileName  
  53.             'cerramos todo  
  54.             mciExecute "Close All"  
  55.             'Para Habilitar y deshabilitar botones  
  56.             Habilitar "Stop"  
  57.         End If  
  58.     End With  
  59. End Sub  
  60.   
  61. 'Sub que obtiene el path corto del archivo a reproducir  
  62. Private Sub PathCorto(archivo As String)  
  63. Dim temp As String * 250 'Buffer  
  64.     path = String(255, 0)  
  65.     'Obtenemos el Path corto  
  66.     ret = GetShortPathName(archivo, temp, 164)  
  67.     'Sacamos los nulos al path  
  68.     path = Replace(temp, Chr(0), "")  
  69. End Sub  
  70.   
  71. 'Procedimiento que ejecuta el comando con el Api mciExecute  
  72. '************************************************************  
  73. Private Sub ejecutar(comando As String)  
  74.     If path = "" Then MsgBox "Error", vbCritical: Exit Sub  
  75.     'Llamamos a mciExecute pasandole un string que tiene el comando y la ruta  
  76.   
  77.     mciExecute comando & path  
  78.   
  79. End Sub  
  80.   
  81. Private Sub Form_Load()  
  82.     Command1.Caption = "Play >>"  
  83.     Command2.Caption = "Stop ||||"  
  84.     Command3.Caption = "Pause ||"  
  85.     Command4.Caption = ":::: Abrir archivo de música ::::"  
  86.     Habilitar "Iniciar"  
  87.     Label1 = "": Label1.AutoSize = True  
  88. End Sub  
  89.   
  90. Private Sub Habilitar(Accion As String)  
  91.     Select Case Accion  
  92.         Case "Iniciar"  
  93.             Command1.Enabled = False  
  94.             Command2.Enabled = False  
  95.             Command3.Enabled = False  
  96.         Case "Play"  
  97.             Command1.Enabled = False  
  98.             Command2.Enabled = True  
  99.             Command3.Enabled = True  
  100.         Case "Stop"  
  101.             Command1.Enabled = True  
  102.             Command2.Enabled = False  
  103.             Command3.Enabled = False  
  104.         Case "Pause"  
  105.             Command1.Enabled = True  
  106.             Command2.Enabled = True  
  107.             Command3.Enabled = False  
  108.     End Select  
  109. End Sub






0 comments:

Post a Comment