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;
}
w.close();



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
    {
        [DllImport("winmm.dll")]
        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, 
       IntPtr.Zero); 
 
       mciSendString("close wave", null, 0, IntPtr.Zero);

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

       return length;
      }
    }
}


 
 
References:

http://stackoverflow.com/questions/82319/how-can-i-determine-the-length-of-a-wav-file-in-c
http://msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement.aspx

For Video Files:

http://forums.asp.net/p/1245309/2295602.aspx
 
 
 

No comments:

Post a comment

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

back to top