Saturday, 24 November 2012

Find the length/Duration of Audio File

Find the length/Duration of Audio File

Method 1:

You will need to reference Windows Media Player. Go to Com Add-ins to add the wmp.dll to your project.

string Duration = null;
WMPLib.WindowsMediaPlayer w = new WMPLib.WindowsMediaPlayer();
WMPLib.IWMPMedia mediaFile = w.newMedia(Filename);
if (mediaFile != null) {
   Duration = mediaFile.durationString;

Method 2:

Add Reference to Shell32.dll.

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; } 

Method 3:

using System;
using System.Text;
using System.Runtime.InteropServices;
namespace Sound
    public static class SoundInfo
        private static extern uint mciSendString(
            string command,
            StringBuilder returnValue,
            int returnLength,
            IntPtr winHandle);

   public static int GetSoundLength(string fileName)
       StringBuilder lengthBuf = new StringBuilder(32);

       mciSendString(string.Format("open \"{0}\" type waveaudio alias wave", 
       fileName),null, 0, IntPtr.Zero); 
       mciSendString("status wave length", lengthBuf, lengthBuf.Capacity, 
       mciSendString("close wave", null, 0, IntPtr.Zero);

       int length = 0; 
       int.TryParse(lengthBuf.ToString(), out length);

       return length;


For Video Files:

No comments:

Post a Comment

Thank You for Your Comments. We will get back to you soon.

back to top