VB NET: MP3 länge auslesen
VB.NET-Quellcode
- Imports System
- Imports System.Collections.Generic
- Imports System.Text
- Imports Shell32
- Public Class Form1
- Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
- Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Integer) As Integer
- Private Function GetMP3Length(ByVal strFileName As String) As Long
- Dim strBuffer As String
- Dim lRet As Long
- Dim sReturn As String
- ' Da die mciSendString Funktion mit langen Dateinamen
- ' nicht korrekt arbeitet, muss zuvor der kurze
- ' 8.3 Dateiname der MP3-Datei ermittelt werden.
- strBuffer = Space$(255)
- lRet = GetShortPathName(strFileName, strBuffer, Len(strBuffer))
- If lRet <> 0 Then
- strFileName = Microsoft.VisualBasic.Left(strBuffer, InStr(strBuffer, vbNullChar) - 1)
- End If
- ' MP3-Datei öffnen
- mciSendString("open " & strFileName & _
- " type MPEGVideo alias mp3audio", 0, 0, 0)
- ' Länge der Datei in Millisekunden auslesen
- sReturn = Space$(256)
- lRet = mciSendString("status mp3audio length", _
- sReturn, Len(sReturn), 0&)
- ' MP3-Datei schliessen
- mciSendString("close mp3audio", 0, 0, 0)
- GetMP3Length = Val(sReturn)
- End Function
- Private Function FormatTime(ByVal lMSec As Long) _
- As String
- Dim iMin As Integer
- Dim iSec As Integer
- iSec = Int(lMSec / 1000)
- iMin = Int(iSec / 60)
- iSec = iSec - (iMin * 60)
- FormatTime = Format$(iMin, "00") & ":" & _
- Format$(iSec, "00")
- End Function
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- MsgBox(FormatTime(GetMP3Length("C:\000.mp3"))) 'wird länge MP3 angezeigt
- End Sub
- End Class