MP3Player
Librer�a para manejar los MP3 usando MP3.dll
Reproductor *experimental*
de archivos MPEG Layer III,
usando la librer�a mp3.dll y empleando t�cnicas de
MultiThreading y Subclasificaci�n... (vaya tela! :-)
Fecha: 23/Sep/98
(5/Sep/98)
Autor: German Oltra [email protected]
Un poco de historia:
Fecha: mi�rcoles
26 de agosto de 1998 22:50
Asunto: Threads
Hola Guillermo...
Gracias por tu respuesta sobre el tema de los threads, entre tu y Pedro
Maicas me habeis orientado y lo he logrado!.
Entre otras cosas he podido domesticar la mp3.dll lanzandola en un thread
independiente y luego poder parar la ejecucion etc...
Si no tienes eso en tu web, quizas te gustaria como colaboracion.
Asi que recibe un cordial saludo ;-)
Fecha: jueves 27
de agosto de 1998 15:05
Asunto: RE: Threads
Pues ser�a interesante.
Enviame el c�digo y un poco de explicaci�n y lo pondr� en las colaboraciones
y en la secci�n VB-Avanzado.
Adem�s eso del MP3 puede que le interese a m�s de uno...
Gracias por adelantado.
Fecha: s�bado 5
de septiembre de 1998 14:06
Asunto: RE: Threads
Bueno Guillermo, pues como lo prometido es deuda aqu� te mando el
ejemplillo de la mp3.dll con multithreading y subclasificacion a saco. Ya
me contaras que te parece.
Si te parece, pones un post diciendo que tienes el ejemplo y donde
encontrarlo, que le interesa a mucha pe�a.
Bueno colega... saludos y hasta otra! :-)
La presentaci�n:
Hola pe�a VBera
:-), para poder ejecutar el ejemplillo es necesario que copieis
el archivo mp3.dll en el directorio c:\windows\system. Y ya ta!
No he tenido tiempo de complicarme la vida en el, asi que faltan
muchas cosas... pero creo que lo principal esta ya hecho. Si le
a�adis mejoras me las mandais eh???.
German Oltra
[email protected]
El c�digo:
Un trocito de c�digo con la declaraci�n de las funciones y algo m�s:
'Copia la DLL en el directorio Windows\system\
Type PlayRecord
FileName As String
SeekAtStart As Long
Owner As Long
Result As Long
End Type
Declare Sub mp3gettime Lib "MP3.DLL" (DATA As PlayRecord, total As Double, perframe As Double)
Declare Sub mp3play Lib "MP3.DLL" (DATA As PlayRecord)
Declare Sub mp3stop Lib "MP3.DLL" ()
Declare Sub mp3seek Lib "MP3.DLL" (position As Integer)
Dim PR as PlayRecord
PR.FileName="c:\mp3\...."
Call mp3play(PR)
El c�digo del form de ejemplo y de los m�dulos BAS:
'El formulario (frmPlayer.frm)
Option Explicit
'====================================================================
' --------------------------------
' M P 3 - P L A Y E R
' --------------------------------
'
' - Reproductor *experimental* de archivos MPEG Layer III, usando
' la librer�a mp3.dll y empleando t�cnicas de MultiThreading y
' Subclasificaci�n... (vaya tela! :-)
'
'
' Germ�n Oltra ICQ#11765971
' http:\\goltra.home.ml.org <[email protected]>
'====================================================================
Private Total As Double
Private PerFrame As Double
Private ProgressTop As Integer
Private ProgressValue As Integer
Private Playing As Boolean
'Gestiona las acciones sobre los controles
Private Sub botones_Click(Index As Integer)
Select Case Index
Case 0
PlayFile
SetControls False, True, True, True, False
Playing = True
Case 1, 2: MsgBox "Las funciones de Seek no las he implementado, eso os lo dejo a vosotros! :-)"
Case 3
StopFile
SetControls True, False, False, False, True
ProgressValue = 0
ProgressBar.AutoRedraw = True
ProgressBar.Cls
ProgressBar.AutoRedraw = False
LCD.Caption = InTime(Total)
Playing = False
Case 4
Dialog.ShowOpen
If Dialog.FileName = "" Then Exit Sub
Mp3Info.FileName = Dialog.FileName
Me.Caption = GetFileName(Dialog.FileName)
SetControls True, False, False, False, True
GetTime Total, PerFrame
ProgressTop = Fix((Total / PerFrame) / 16)
ProgressValue = 0
LCD.Caption = InTime(Total)
End Select
End Sub
'Secuencia de inicializaci�n
Private Sub Form_Load()
HookForm Me
Mp3Info.Owner = Me.hWnd
Playing = False
SetControls False, False, False, False, True
End Sub
'Detener la interceptaci�n de mensajes
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
unHookForm
End Sub
'No puedo salir si se esta reproduciendo
Private Sub Form_Unload(Cancel As Integer)
If Playing Then
StopFile
Playing = False
Cancel = 1
End If
End Sub
'Activa-Desactiva los botones
Private Sub SetControls(ByVal bPlay As Boolean, ByVal bRewind As Boolean, _
ByVal bForward As Boolean, ByVal bStop As Boolean, ByVal bEject As Boolean)
Botones(0).Enabled = bPlay
Botones(1).Enabled = bRewind
Botones(2).Enabled = bForward
Botones(3).Enabled = bStop
Botones(4).Enabled = bEject
End Sub
'Avanza la barra de progreso
Private Sub PushBar()
Dim Contador As Integer
ProgressBar.AutoRedraw = True
For Contador = 1 To Fix(ProgressBar.ScaleWidth / ProgressTop)
ProgressBar.Line (ProgressValue + Contador, 0)-Step(0, ProgressBar.ScaleHeight)
Next Contador
ProgressBar.AutoRedraw = False
ProgressValue = ProgressValue + Contador
End Sub
'Destino de los mensajes interceptados
Public Sub MessageReceived(ByVal Message As Long, ByVal wParam As Long, ByVal lParam As Long)
Select Case Message
Case FRAME_POS: LCD.Caption = InTime(Total - (PerFrame * wParam)): PushBar
Case APPLY_POS: Debug.Print Now, "Seek Process Done!"
Case PLAY_STOP: SetControls True, False, False, False, True: Playing = False
End Select
End Sub
'Muestra o esconde el *about*
Private Sub LCD_Click()
frmPlayer.Height = IIf(frmPlayer.Height = 3615, 1710, 3615)
End Sub
'El m�dulo: MP3Control.bas
Option Explicit
'====================================================================
' --------------------------------------------
' MODULO DE CONTROL PARA LA LIBRERIA MP3.DLL
' --------------------------------------------
'
' - Reproducci�n de los archivos MP3 en THREADS independientes
' para mantener en todo momento el control sobre el proceso.
'
'
' Germ�n Oltra ICQ#11765971
' http:\\goltra.home.ml.org <[email protected]>
'====================================================================
'Tipos definidos
Private Type PlayRecord
FileName As String
SeekAtStart As Long
Owner As Long
Result As Long
End Type
'Constantes Globales
Public Const FRAME_POS = &H2EE0
Public Const APPLY_POS = &H2EE1
Public Const PLAY_STOP = &H2EE2
'Variables Globales
Public Mp3Info As PlayRecord
'Funciones Importadas de "mp3.dll"
Private Declare Sub mp3gettime Lib "MP3.DLL" (DATA As PlayRecord, Total As Double, PerFrame As Double)
Private Declare Sub mp3play Lib "MP3.DLL" (DATA As PlayRecord)
Private Declare Sub mp3stop Lib "MP3.DLL" ()
Private Declare Sub mp3seek Lib "MP3.DLL" (Position As Integer)
'Funciones Importadas del API
Private Declare Function CreateThread Lib "kernel32" (ByVal Null1 As Long, ByVal Null2 As Long, ByVal StartAddress As Long, Parameter As Any, ByVal Null3 As Long, ThreadId As Long) As Long
'Reproduce el archivo
Private Sub StartSong()
mp3play Mp3Info
End Sub
'Reproduce el archivo en un thread independiente
Public Sub PlayFile()
Dim Identifier As Long
CreateThread 0, 0, AddressOf StartSong, 0, 0, Identifier
End Sub
'Obtiene la duraci�n del archivo
Public Sub GetTime(Total As Double, PerFrame As Double)
mp3gettime Mp3Info, Total, PerFrame
End Sub
'Situa la reproducci�n en un punto concreto
Public Sub JumpTo(Frame As Integer)
mp3seek Frame
End Sub
'Para la reproducci�n
Public Sub StopFile()
mp3stop
End Sub
'M�dulo: PrivateEye.bas
Option Explicit
'====================================================================
' ------------------------------------------
' MODULO DE INTERCEPTACION DE MENSAJES
' ------------------------------------------
'
' - Utilizado para "recoger" los mensajes generados por la
' libreria mp3.dll, y as� poder reaccionar al respecto.
'
'
' Germ�n Oltra ICQ#11765971
' http:\\goltra.home.ml.org <[email protected]>
'====================================================================
'Variables globales en el m�dulo
Private TargetForm As Form
Private TargetHandle As Long
Private PrevWndProc As Long
Private Const GWL_WNDPROC = (-4&)
'Funciones Importadas del API
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal MSG As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
'Aqu� se reciben los mensajes
Private Function WndProc(ByVal hWnd As Long, ByVal uMSG As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
WndProc = CallWindowProc(PrevWndProc, hWnd, uMSG, wParam, lParam)
If Not TargetForm Is Nothing Then TargetForm.MessageReceived uMSG, wParam, lParam
End Function
'Inicia la interceptaci�n de mensajes
Public Sub HookForm(ByVal Target As Form)
If Not TargetForm Is Nothing Then unHookForm
Set TargetForm = Target
TargetHandle = Target.hWnd
PrevWndProc = SetWindowLong(TargetHandle, GWL_WNDPROC, AddressOf WndProc)
End Sub
'Detiene la interceptaci�n de mensajes
Public Sub unHookForm()
If TargetHandle <> 0 Then SetWindowLong TargetHandle, GWL_WNDPROC, PrevWndProc
End Sub
'M�dulo: AuxFuncs.bas
Option Explicit
'====================================================================
' ----------------------------------------
' FUNCIONES AUXILIARES
' ----------------------------------------
'
' - Otras funciones utilizadas para la conversi�n de datos
'
' Germ�n Oltra ICQ#11765971
' http:\\goltra.home.ml.org <[email protected]>
'====================================================================
'Convierte milisegundos --> String hora
Public Function InTime(ByVal Milisecs As Double) As String
Dim Minutes As Integer, Seconds As Integer
Minutes = 0
Seconds = Fix(Milisecs / 1000)
While (Seconds > 59)
Seconds = Seconds - 60
Minutes = Minutes + 1
Wend
InTime = Format(Minutes, "00") & ":" & Format(Seconds, "00")
End Function
'Extrae el nombre de archivo de una ruta completa
Public Function GetFileName(ByVal Path As String) As String
Dim Contador As Integer
Contador = 1
While Mid(Path, Len(Path) - Contador, 1) <> "\"
Contador = Contador + 1
Wend
GetFileName = Mid(Path, Len(Path) - Contador + 1)
End Function
Bajate el fichero con el c�digo de ejemplo y la librer�a (MP3Player.zip 84 KB)