下载dll: 点击下载
添加引用dll Interop.Shell32.dll///
/// 获取语音文件大小
///
/// 语音文件
/// 秒
public static int GetVoiceDuration(string fileNamePath)
{
int duration = 0;
try
{
string dirName = Path.GetDirectoryName(fileNamePath);
string SongName = Path.GetFileName(fileNamePath);
FileInfo fInfo = new FileInfo(fileNamePath);
ShellClass sh = new ShellClass();
Folder dir = sh.NameSpace(dirName);
FolderItem item = dir.ParseName(SongName);
string VoiceDuration = Regex.Match(dir.GetDetailsOf(item, -1), "\\d:\\d{2}:\\d{2}").Value;
string[] voiceSplit = VoiceDuration.Split(':');
if (voiceSplit.Length > 2)
{
//这里获取的秒(需要获取时,分,另外处理,VoiceDuration返回结果: 00:00:12)
duration = Convert.ToInt32(voiceSplit[2]);
}
}
catch (Exception ex)
{
G2Log.Error("获取语音文件大小失败", ex);
}
return duration;
}
如果提示 System.InvalidCastException: 无法将类型为“Shell32.ShellClass”的 COM 对象强制转换为接口类型“Shell32.IShellDispatch6”。此操作失败的原因是对 IID 为“{286E6F1B-7113-4355-9562-96B7E9D64C54}”的接口的 COM 组件调用 QueryInterface 因以下错误而失败,则需要选中刚引用的Dll文件,属性设置"嵌入式操作类型"为"false"
有些服务器无法通过ShellClass获取mp3时长,原因如下:
服务器和我们使用的系统不同,有些软件和配置默认是不安装,需要我们手动去安装,因为服务器不需要用户桌面体验,毕竟又不常操作次系统,所以服务器是不默认安装“优质windows音频视频体验”和“桌面体验”。而win7是默认安装。
解决方法(此方法来自csdn,经测试有效):
开始菜单=》管理工具=》服务器管理器=》功能=》添加功能,找到“优质windows音频视频体验”和“桌面体验”勾选即可。