Здравствуйте, Lesnix, Вы писали:
L>Необходимо получить длительность файла МР3.
L>Варианты могут быть не только на VB, но и на C и РНР
L>Очень надо... Сделать это надо без использования компонента MediaPlayer B Visual Basic...
L>Подскажите, как
Очень просто, через MCI (Multimedia Control Interface):
'в форму, на нее Command1, Command2, Text1
Option Explicit
Dim mci01 As clsMCI
Private Sub Command1_Click()
On Error Resume Next
mci01.OpenMCI Text1.Text
If mci01.MCIError <> False Then Exit Sub
MsgBox "Длина композиции " & Trim(Str(mci01.Length)) & " секунд."
End Sub
Private Sub Command2_Click()
mci01.CloseMCI
End
End Sub
Private Sub Form_Load()
Set mci01 = New clsMCI
End Sub
'в класс clsMCI
Option Explicit
Private Declare Function mciExecute Lib "winmm.dll" (ByVal lpstrCommand As String) As Long
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" (ByVal dwError As Long, ByVal lpstrBuffer As String, ByVal uLength As Long) As Long
Private Declare Function auxSetVolume Lib "winmm.dll" (ByVal uDeviceID As Long, ByVal dwVolume As Long) As Long
Private Declare Function auxGetVolume Lib "winmm.dll" (ByVal uDeviceID As Long, lpdwVolume As Long) As Long
Public MCIFilename As String
Public MediaLoaded As Boolean
Public hwnd As Long
Private FResult As Long
Private MCIAlias As String
Private mciHWND As Long
Private mciCaption As String
Public MCIError As Boolean
Public From As Long
Public ToPos As Long
Public Fullscreen As Boolean
Public ShowErrors As Boolean
Private bPlaying As Boolean
Private bPaused As Boolean
Public Function VBMCISendString(ByVal strCommand As String) As String
Dim strTemp As String, lngLen As Long, lRes As Long
strTemp = String$(128, 32): lngLen = 128
lRes = mciSendString(strCommand, strTemp, lngLen, hwnd)
FResult = lRes
If lRes <> 0 Then
If ShowErrors = True Then MsgBox VBMCIGetErrorString(lRes), vbCritical, MCIAlias
MCIError = True
End If
VBMCISendString = Trim$(Left$(strTemp, lngLen))
End Function
Public Sub OpenMCI(ByVal FileName As String)
MCIFilename = FileName
If MediaLoaded = True Then CloseMCI
VBMCISendString "open " & Chr$(34) & FileName & Chr$(34) & " alias " & MCIAlias
MediaLoaded = True
bPlaying = False
End Sub
Public Function VBMCIGetErrorString(ByVal ErrorCode As Long) As String
Dim strTemp As String, lngLen As Long
strTemp = String$(128, 32): lngLen = 128
mciGetErrorString ErrorCode, strTemp, lngLen
VBMCIGetErrorString = Trim$(Left$(strTemp, lngLen))
If Len(Trim$(strTemp)) > 0 Then MCIError = True
End Function
Private Sub Class_Initialize()
ShowErrors = True
Randomize Timer
MCIAlias = "MCICLASS_DEVICE_" & CStr(CInt(Rnd * 200))
End Sub
Private Sub Class_Terminate()
VBMCISendString "close " & MCIAlias
'CloseAll
End Sub
Public Sub Play()
Dim strTemp As String
If Not bPaused Then
strTemp = "play " & MCIAlias
If From <> 0 Then strTemp = strTemp & " from " & From
If ToPos > From Then strTemp = strTemp & " to " & ToPos
If Fullscreen = True Then strTemp = strTemp & " fullscreen"
Else
strTemp = "resume " & MCIAlias
bPaused = False
End If
VBMCISendString strTemp
bPlaying = True
End Sub
Public Property Get Position() As Long
Position = Val(VBMCISendString("status " & MCIAlias & " position "))
End Property
Public Property Let Position(ByVal NewVal As Long)
If NewVal < 0 Then NewVal = 0
If NewVal > Length Then NewVal = Length
VBMCISendString "seek " & MCIAlias & " to " & NewVal
If bPlaying = True Then Play
End Property
Public Property Get Length() As Long
Length = Val(VBMCISendString("status " & MCIAlias & " length "))
End Property
'Public Property Get WindowHandle() As Long
'End Property
Public Property Let WindowHandle(ByVal vNewValue As Long)
VBMCISendString "window " & MCIAlias & " handle " & vNewValue
End Property
'Public Property Get WindowCaption() As string
'End Property
Public Property Let WindowCaption(ByVal vNewValue As String)
VBMCISendString "window " & MCIAlias & " text " & Chr$(34) & vNewValue & Chr$(34)
End Property
Public Property Get CanPlay() As Boolean
VBMCISendString "capability " & MCIAlias & " can play"
If FResult = 0 Then CanPlay = True Else CanPlay = True
If MCIError = True Then CanPlay = False
MCIError = False
End Property
Public Property Get TimeFormat() As String
TimeFormat = VBMCISendString("status " & MCIAlias & " time format")
End Property
Public Property Let TimeFormat(ByVal vNewValue As String)
VBMCISendString "set " & MCIAlias & " time format " & vNewValue
End Property
Public Sub StopMCI()
VBMCISendString "stop " & MCIAlias
bPaused = False
bPlaying = False
End Sub
Public Sub CloseMCI()
StopMCI
VBMCISendString "close " & MCIAlias
MediaLoaded = False
bPlaying = False
MCIFilename = vbNullString
End Sub
Public Property Get DeviceType() As String
DeviceType = VBMCISendString("capability " & MCIAlias & " device type ")
End Property
Public Sub MoveEnd()
VBMCISendString "seek " & MCIAlias & " to end"
If bPlaying = True Then Play
End Sub
Public Sub MoveStart()
VBMCISendString "seek " & MCIAlias & " to start"
If bPlaying = True Then Play
End Sub
Public Sub Pause()
VBMCISendString "pause " & MCIAlias
bPaused = True
End Sub
Public Sub StepMCI(ByVal StepSize As Long)
'Dim TFrom As Long
'TFrom = Position + StepSize
'If bPlaying = True Then
' VBMCISendString "play " & MCIAlias & " from " & TFrom
'Else
' Position = TFrom
'End If
End Sub
Public Sub CloseAll()
Dim i As Long
For i = 0 To 200
VBMCISendString "close MCICLASS_DEVICE_" & CStr(i)
Next i
End Sub
... << RSDN@Home 1.1.3 stable >>