MP3ファイルの再生時間を取得          <TOP>


mciSendString 文字列を MCI(Multimedia Control Interface) に送信

GetShortPathName ファイルの短い形式のパス名を取得

 

左:ファイルを開き再生時間を取得    右:確認

 

 

'================================================================
'= MP3ファイルの再生時間を取得
'=    (mciSendString2.bas)
'================================================================
#include "Windows.bi"

' 文字列を MCI に送信
Declare Function Api_mciSendString& Lib "winmm" Alias "mciSendStringA" (ByVal lpstrCommand$, ByVal lpstrReturnString$, ByVal uReturnLength&, ByVal hwndCallback&)

' ファイルの短い形式のパス名を取得
Declare Function Api_GetShortPathName& Lib "Kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath$, ByVal lpszShortPath$, ByVal lBuffer&)

Var Shared Text(2) As Object
For i = 0 To 2
    Text(i).Attach GetDlgItem("Text" & Trim$(Str$(i + 1)))
    Text(i).SetFontSize 14
Next

Var Shared FileName As String

'================================================================
'=
'================================================================
Declare Function GetMP3Length(FileName As String) As Long
Function GetMP3Length(FileName As String) As Long
    Var Buffer As String
    Var Length As String
    Var Ret As Long

    Buffer = space$(255)
    Ret = Api_GetShortPathName(FileName, Buffer, Len(Buffer))
  
    If Ret <> 0 Then
        FileName = Left$(Buffer, InStr(Buffer, Chr$(0)) - 1)
    End If
  
    Ret = Api_mciSendString("open " & FileName & " Type MPEGVideo Alias mp3audio", ByVal 0, 0, 0)
     
    Length = space$(256)
    Ret = Api_mciSendString("status mp3audio length", Length, Len(Length), 0)
    
    Ret = Api_mciSendString("close mp3audio", ByVal 0, 0, 0)
  
    GetMP3Length = Val(Length)
End Function

'================================================================
'=
'================================================================
Declare Function FormatTime(ByVal fTime As Long) As String
Function FormatTime(ByVal fTime As Long) As String
    Var Min As Integer
    Var Sec As Integer
  
    Sec = int(fTime / 1000)
    Min = int(Sec / 60)
    Sec = Sec - (Min * 60)
  
    FormatTime = Format$(Min, "##") & "分" & Format$(Sec, "##") & "秒"
End Function

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()

    FileName = WinOpenDlg("ファイルのオープン","C:\*.mp3","MP3ファイル(*.mp3)", 0)
    If FileName <> Chr$(&H1B) Then
        Text(0).SetWindowText FileName
    End If
End Sub

'================================================================
'=
'================================================================
Declare Sub Button2_on edecl ()
Sub Button2_on()

    Text(1).SetWindowText FormatTime(GetMP3Length(FileName))
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End