How to get duration of an audio file in c# ?

We can get the duration of an audio or video file by using the “Shell32” namespace in Visual Studio Dot Net. For that we need to add the reference of “Interop.Shell32.dll” in to our project. And follow the given method in below. By calling the following method with the parameter of full path of file name which might be an audio file or video file we can get the duration of that file.

private string GetDuration(string FileFullPath)
{
string duration = "";
string fName = FileFullPath.Substring(FileFullPath.LastIndexOf("\\") + 1);
string filePath = FileFullPath.Substring(0, FileFullPath.LastIndexOf("\\"));
Shell32.Shell shell = new Shell32.ShellClass();
Shell32.Folder folder = shell.NameSpace(filePath);
Shell32.FolderItem folderItem = folder.ParseName(fName);
if (folderItem != null)
{
duration = folder.GetDetailsOf(folderItem, 21);
}
folderItem = null;
folder = null;
shell = null;
return duration;
}

Hence we can get the duration of given file.

…S.VinothkumaR.

7 comments:

Dinesh Audichya said...

very good idea so simple and direct approach

Unknown said...

Really appreciated the solution. I was looking for it without any DirectX support

rahuldeo2047 said...
This comment has been removed by the author.
rahuldeo2047 said...
This comment has been removed by the author.
siraj said...

am not getting the duration for .wav and .mpg

Unknown said...

hey its not working for .wav,wma.m4a etc...pls find the code for it...

Kishore k said...

hey its not working for .flv file etc...pls help me..how to get duration of the video files