C#中获取音频文件时长

时间:2023-12-14 20:16:20

1.在项目中添加引入:COM组件的Microsoft Shell Controls And Automation

2.在引用中找到Shell32,点击右键,在属性中将“嵌入互操作类型”的值改为“false”

3.代码

 public static string GetVoiceTime(string voicePath)//voicePath为语音文件路径
{
string path = HttpContext.Current.Server.MapPath(voicePath);//将虚拟路径转换为绝对路径,这里必须用绝对路径
string dirName = Path.GetDirectoryName(path);//获取文件夹名称
string voiceName = Path.GetFileName(path);//获取文件名
FileInfo file = new FileInfo(path);
ShellClass sh = new ShellClass();
Folder dir = sh.NameSpace(dirName);
FolderItem item = dir.ParseName(voiceName);
string songTime = dir.GetDetailsOf(item, );//win7参数为27
return songTime;
}