The headaches of using using or not using Shell32

 
After much frustration trying to find information on or ready made code on how to (successfully) read MP3 headers and get the play time, I stumbled on a trick someone had used – using Shell32.dll
 
I didn’t then have much immediate joy finding documentation on the methods, but ended up wrapping the call to the DLL inside another DLL, embedded the logic in this, then in turn found I was able to get the play time quite nicely.
 
using System;
using System.Collections.Generic;
using System.IO;
using Shell32;
namespace MPEGHeaderLib
{
    /// <summary>
    /// returns the play time / duration of a file., eg MP3 or AVI
    /// </summary>
    public class MpegInfo
    {
        /// <summary>
        /// returns the play time / duration of a file., eg MP3 or AVI
        /// </summary>
        /// <param name="sFile">filename and path</param>
        /// <returns>integer representing play time in seconds</returns>
        public int GetDuration(string sFile)
        {
            int playtime = 0;
            if (sFile.Length > 0)
            {
                try
                {
                    // Creating a ShellClass Object from the Shell32
                    ShellClass sh = new ShellClass();
                    // Creating a Folder Object from Folder that inculdes the File
                    Folder dir = sh.NameSpace(Path.GetDirectoryName(sFile));
                    // Creating a new FolderItem from Folder that includes the File
                    FolderItem item = dir.ParseName(Path.GetFileName(sFile));
                    // loop throw the Folder Items
                    string tmhms = dir.GetDetailsOf(item, 21).Trim();
                    string[] tms;
                    tms = tmhms.Split(
‘:’);
                    int hh = int.Parse(tms[0]);
                    int mm = int.Parse(tms[1]);
                    int ss = int.Parse(tms[2]);
                    playtime = ((hh * 60) + mm) * 60 + ss;
                }
                catch (Exception)
                {}
            }
            return playtime;
        }
    }
}

add a reference in your project to c:\windows\system32\shell32.dll and build this into a DLL

this is how I then use it in DotNetNuke module:

/// <summary>
/// returns integer representing the duration in seconds
/// </summary>
/// <param name="UploadPath">UploadPath</param>
/// <param name="Filename">Filename</param>
/// <returns>file duration in seconds</returns>
protected int GetFileDuration(string RootPath, string SelFileName)
{
    int retval = 0;
    if (RootPath.Trim() != "" && SelFileName.Trim() != "")
    {
        try
        {
            MPEGHeaderLib.
MpegInfo mi = new MPEGHeaderLib.MpegInfo();
            retval = mi.GetDuration(RootPath + SelFileName);
        }
        catch { }
    }
    return retval;
}

add a reference to this new wrapper DLL too to DNN

apart from personal perpiration, sources and inspiration were due in large part to ideas in this article:
http://www.codeproject.com/KB/files/detailedfileinfo.aspx on Code Project

(sorry if I have missed some – please tell me).

 

Advertisements