Is there anyway to get the length of an MP3 or WMA file without opening the file. Windows explorer shows that information, so I would assume that is available though .NET.
Nothing in FileInfo or Attributes had what I was looking for.
Any ideas?
Printable View
Is there anyway to get the length of an MP3 or WMA file without opening the file. Windows explorer shows that information, so I would assume that is available though .NET.
Nothing in FileInfo or Attributes had what I was looking for.
Any ideas?
When you say length, do you mean the time required ?
Sorry, let me specify. I mean total length or total time of the song.
Use MCIsendStrings
VB Code:
Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" ( _ ByVal lpstrCommand As String, _ ByVal lpstrReturnString As String, _ ByVal uReturnLength As Int32, _ ByVal hwndCallback As Int32) As Int32 Private Sub OpenMedia(ByVal fname As String) As String 'loads the file _FileName = fname _alias = System.Guid.NewGuid.ToString.Replace("-"c, "") Dim cmd As String = "open " & _FileName & " alias " & _alias mciSendString(cmd, retVal, retLength, 0) End Function Private Sub GetLength() 'gets the length of the loaded file Dim retVal As New String("X"c, 128) Dim cmd As String = "status " & _alias & " length" mciSendString(cmd, retVal, 128, 0) If Double.TryParse(l, _Length) = False Then _Length = -1 End If End Sub
I didn't test this. I took it from a media player that was geared for video I was writing, so there may be some errors. But that and this, should get you through it I would think.
There has to be a better way than that? Doesn't that just get the file length, not the time (in minutes and seconds) of the song?
Does anyone know of an easier way to get the time of an MP3? If not, I think I might just use the built-in method in .NET to get the total size of the file and just estimate the length of the file since I don't need the exact amount.
That does get the time ( I think in nano seconds), but I may just be thinking of the project I'm working on right now that uses DirectShow Editing. Another way you could do it is us a mediaplayer component. But that's just adding overhead since WMP is based off of SendStrings