VB.NET get mp3 file duration...

LittleOleMeDesigns used Ask the Experts™
I am trying to find a way with a vb.net program to retrieve the file duration (not file size) of an .mp3 file.  Does anyone know how to do this?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®


for some reason, that link isn't working for me... any chance you could post what it says?

Idea 1.
Add a reference to "Windows Media Player" -- It's under the COM add-ins.
 Use the one which references "wmp.dll".  Then use this code:
Dim Duration as string
Dim w As New WMPLib.WindowsMediaPlayer
Dim m As WMPLib.IWMPMedia = w.newMedia(Filename)
If m IsNot Nothing Then
Duration = m.durationString
End If

Idea  2:

checking links

Idea 3:
  This is a bit detailed one.. but
GEtting data from mp3 header..
mp3 file format  http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html
duration = total-number-of-frames * samples-per-frame / sample-frequency

How to get these data ?  Create a binary stream...  
read binary data from header and calculate urself..

Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.


Thanks for doing that.  None of those look quite like what I'm needing.

I'm needing to create a program that will go through a given folder and compare files.  If the file name and file duration (not size) of the mp3 files are the same, then delete one of them because it's a duplicate.

that is what ur asking..  mp3 file duration..
and there are 3 different answers of that questions..

for ur need:

here is an MSDN example

 For Directory.GetFiles and Directory.GetDirectories
' For File.Exists, Directory.Exists 
Imports System
Imports System.IO
Imports System.Collections
Public Class RecursiveFileProcessor
    Public Overloads Shared Sub Main(ByVal args() As String)
        Dim path As String
        For Each path In args
            If File.Exists(path) Then
                ' This path is a file.
                If Directory.Exists(path) Then
                    ' This path is a directory.
                    Console.WriteLine("{0} is not a valid file or directory.", path)
                End If
            End If
        Next path
    End Sub 'Main
    ' Process all files in the directory passed in, recurse on any directories 
    ' that are found, and process the files they contain.
    Public Shared Sub ProcessDirectory(ByVal targetDirectory As String)
        Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
        ' Process the list of files found in the directory.
        Dim fileName As String
        For Each fileName In fileEntries
        Next fileName
        Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
        ' Recurse into subdirectories of this directory.
        Dim subdirectory As String
        For Each subdirectory In subdirectoryEntries
        Next subdirectory
    End Sub 'ProcessDirectory
    ' Insert logic for processing found files here.
    Public Shared Sub ProcessFile(ByVal path As String)
        ' Check File name and duration in hear 
    End Sub 'ProcessFile
End Class 'RecursiveFileProcessor

Open in new window


Yeah... I knew how to iterate through items in files and compare them... what I don't know is how to grab the file duration from the file.  I'll try this example and see if I can pull that up.
there is no duration information kept on the file...

I ve told 2 differen ways..
one is using media player com object to retrieve file duration..
other is reading the framecount and calcuting it by urself..
Chose one of it..  


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial