Share via


how to get length of mp3 file

Question

Wednesday, February 12, 2014 2:22 PM

how to get the length of mp3 file using vb.net
I saw some tutorials but only for C# and java.
and some of the tutorials for vb.net is only for title, album and artist NO LENGTH
so how can I get that?

All replies (5)

Wednesday, February 12, 2014 2:39 PM ✅Answered | 1 vote

how to get the length of mp3 file using vb.net
I saw some tutorials but only for C# and java.
and some of the tutorials for vb.net is only for title, album and artist NO LENGTH
so how can I get that?

Option Strict Off

'Add reference browse C:\Windows\System32\Shell32.Dll

Public Class Form1

    Dim Path As String = ""
    Dim FileName As String = ""

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.CenterToScreen()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        RichTextBox1.Text = ""
        Dim OFD As New OpenFileDialog
        OFD.Multiselect = False
        If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
            Path = OFD.FileName
            Label1.Text = Path
            Dim x As Integer = Path.LastIndexOf("\"c)
            FileName = Path.Substring(x + 1, Path.Count - x - 1)
            Path = Path.Remove(x, Path.Count - x)
            Dim objShell As Object
            Dim objFolder As Object
            Dim strDimensions As Object
            objShell = CreateObject("Shell.Application")
            objFolder = objShell.Namespace(Path.ToString) ' path to the foldercontaining the file
            strDimensions = objFolder.GetDetailsOf(objFolder.ParseName(FileName), 27) ' Filename use 27 for video or audio length
            Try
                For i = 1 To 1000
                    RichTextBox1.AppendText(i.ToString & " .. " & objFolder.GetDetailsOf(objFolder.ParseName(FileName), i) & vbCrLf)
                Next
            Catch ex As Exception
            End Try
            Label2.Text = "Files duration is " & strDimensions.ToString
        End If
    End Sub

End Class

Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


Wednesday, February 12, 2014 4:25 PM ✅Answered | 1 vote

Hi,

 After a little tinkering with Mr. Monkeyboys code i figured out how to get it to work without turning option strict off which i don`t like to do, so i will post it. Be aware that not all audio files will return the same info so if you want info from other audio type files you will need to adjust the code to retrieve those types. This is good for mp3 files the way i have this set up.

'Add reference browse C:\Windows\System32\Shell32.Dll

Public Class Form1

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        RichTextBox1.Text = ""
        Using OFD As New OpenFileDialog
            OFD.Multiselect = False
            If OFD.ShowDialog = DialogResult.OK Then
                Dim FolderPath As String = IO.Path.GetDirectoryName(OFD.FileName)
                Dim FileName As String = IO.Path.GetFileName(OFD.FileName)
                Dim objShell As Shell32.Shell = CType(CreateObject("Shell.Application"), Shell32.Shell)
                Dim objFolder As Shell32.Folder = CType(objShell.NameSpace(FolderPath.ToString), Shell32.Folder)

                Try
                    For i = 0 To 1000
                        RichTextBox1.AppendText(i.ToString & " .. " & objFolder.GetDetailsOf(objFolder.ParseName(FileName), i) & vbCrLf)
                    Next
                Catch ex As Exception
                End Try

                'These are for mp3 files only
                Label1.Text = "File Name: " & objFolder.GetDetailsOf(objFolder.ParseName(FileName), 0)
                Label2.Text = "File Size: " & objFolder.GetDetailsOf(objFolder.ParseName(FileName), 1)
                Label3.Text = "Duration: " & objFolder.GetDetailsOf(objFolder.ParseName(FileName), 21)
                Label4.Text = "Sample Rate: " & objFolder.GetDetailsOf(objFolder.ParseName(FileName), 33)
                Label5.Text = "Bitrate: " & objFolder.GetDetailsOf(objFolder.ParseName(FileName), 22)
                Label6.Text = "Channels: " & objFolder.GetDetailsOf(objFolder.ParseName(FileName), 34)
                Label7.Text = "Modified Date: " & objFolder.GetDetailsOf(objFolder.ParseName(FileName), 3)
                Label8.Text = "Created Date: " & objFolder.GetDetailsOf(objFolder.ParseName(FileName), 4)
                Label9.Text = "Accessed Date: " & objFolder.GetDetailsOf(objFolder.ParseName(FileName), 5)
            End If
        End Using
    End Sub

End Class


Friday, October 31, 2014 6:22 AM | 1 vote

I know this is late, but since this is a top result in google, i figured i'd add another method for people looking.
The method isn't as nice as above, but works well and is much easier.

After importing WMPLib

 Dim mp As WindowsMediaPlayer = New WindowsMediaPlayer
 Dim song As WMPLib.IWMPMedia = mp.newMedia("File Address")

then you can get the length of a mp3 file using

song.duration

It will be displayed in milliseconds.


Friday, October 31, 2014 6:43 AM

I know this is late, but since this is a top result in google, i figured i'd add another method for people looking.
The method isn't as nice as above, but works well and is much easier.

After importing WMPLib

 Dim mp As WindowsMediaPlayer = New WindowsMediaPlayer
 Dim song As WMPLib.IWMPMedia = mp.newMedia("File Address")

then you can get the length of a mp3 file using

song.duration

It will be displayed in milliseconds.

That's a cool idea. And really thinking outside the box. So simple too.

La vida loca


Saturday, September 15, 2018 11:50 PM

yes but very hard hen u wana add file name and duration to listbox