メディアファイルの再生時間取得          <TOP>


メディアファイル(WAV、AVI、MP3、MIDI)を指定し、その再生時間を取得します。

mciSendString 文字列を MCI に送信

 

左:WAVファイルを選択    右:AVIファイルを選択

 

左:MP3ファイルを選択    右:MIDIファイルを選択

 

※FileNameに空白があることも想定し、chr$(34) & FileName & chr$(34) のようにファイルパスを「"」で囲みます。

 

'================================================================
'= メディアファイルの再生時間取得
'=    (GetMediaLength.bas)
'================================================================
#include "Windows.bi"

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

Var Shared Text(2) As Object
Var Shared Edit1 As Object
Var Shared Button(1) As Object

For i = 0 To 2
    If i < 2 Then
        Button(i).Attach GetDlgItem("Button" & Trim$(Str$(i + 1))) : Button(i).SetFontSize 14
    End If
    Text(i).Attach GetDlgItem("Text" & Trim$(Str$(i + 1))) : Text(i).SetFontSize 14
Next
Edit1.Attach GetDlgItem("Edit1") : Edit1.SetFontSize 14

Var Shared FileName As String

ShowWindow -1

'================================================================
'=
'================================================================
Declare Function GetMediaLength(FileName As String)
Function GetMediaLength(FileName As String)
    Var MediaLength As Long
    Var RetString As String * 256
    Var CommandString As String
    Var Ret As Long

    CommandString = "Open " & FileName & " Alias MediaFile"
    Ret = Api_mciSendString(CommandString, ByVal 0, 0, 0)

    CommandString = "Set MediaFile time format milliseconds"
    Ret = Api_mciSendString(CommandString, ByVal 0, 0, 0)

    CommandString = "Status MediaFile length"
    Ret = Api_mciSendString(CommandString, RetString, Len(RetString), 0)

    GetMediaLength = CLng(Val(RetString))
    CommandString = "Close MediaFile"
    Ret = Api_mciSendString(CommandString, ByVal 0, 0, 0)
End Function

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Text(2).SetWindowText ""

    FileName = WinOpenDlg("ファイルのオープン","*.wav;*.avi;*.mp3;*.mid","WAVファイル(*.wav);AVIファイル(*.avi);MP3ファイル(*.mp3);MIDIファイル(*.mid);全てのファイル(*.*)", 0)

    If FileName <> Chr$(&H1B) Then
        Edit1.SetWindowText FileName
    End If

    FileName = Chr$(34) & FileName & Chr$(34)
End Sub

'================================================================
'=
'================================================================
Declare Sub Button2_on edecl ()
Sub Button2_on()
    If Edit1.GetWindowText() = "" Then Exit Sub
    Var Seconds As Integer
    Var Minutes As Integer
    Var MilliSeconds As Long
    Var TotalTime As String

    MilliSeconds = GetMediaLength(FileName)

    Seconds = Int(MilliSeconds / 1000) Mod 60
    Minutes = Int(MilliSeconds / 60000)
    MilliSeconds = MilliSeconds Mod 1000

    TotalTime = Str$(Minutes) & ":" & Str$(Seconds) & ":" & Str$(MilliSeconds)
    Text(2).SetWindowText TotalTime
End Sub

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