zoukankan      html  css  js  c++  java
  • c# 获取MP3和AMR文件格式的时长

    //网上摘录整理
    private
    long GetAMRFileDuration(string fileName) { long duration = 0; FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); { byte[] packed_size = new byte[16] { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 }; int pos = 0; pos += 6; long lenth = fs.Length; byte[] toc = new byte[1]; int framecount = 0; byte ft; while (pos < lenth) { fs.Seek(pos, SeekOrigin.Begin); if (1 != fs.Read(toc, 0, 1)) { duration = lenth > 0 ? ((lenth - 6) / 650) : 0; fs.Close(); break; } ft = (byte)((toc[0] / 8) & 0x0F); pos += packed_size[ft] + 1; framecount++; } duration = framecount * 20 / 1000; } fs.Close(); return duration; }
        //添加引用:COM组件的Microsoft Shell Controls And Automation
        //然后引用 using Shell32;
        //如果出现“无法嵌入互操作类型 请改用适用的接口”报错————修改引用里面的shell32属性嵌入互操作类型改为false
    private long GetMP3FileDuration(string fileName) { ShellClass sh = new ShellClass(); Folder dir = sh.NameSpace(Path.GetDirectoryName(fileName)); FolderItem item = dir.ParseName(Path.GetFileName(fileName)); string str = dir.GetDetailsOf(item, 27); // 获取歌曲时长。 var matchs = Regex.Match(str, @"(d{2}):(d{2}):(d{2})"); var hour = nt.PubTool.Comm.ConvertTo.ToInt32(matchs.Groups[1].Value); var minute = nt.PubTool.Comm.ConvertTo.ToInt32(matchs.Groups[2].Value); var second = nt.PubTool.Comm.ConvertTo.ToInt32(matchs.Groups[3].Value); var len = hour * 3600 + minute * 60 + second; return len; }
  • 相关阅读:
    js数字格式化(加千分位逗号)
    [css]解决iframe在ios设备上无法滚动
    判断当前是否在微信浏览器环境
    TortoiseGit 提交代码每次需要输入用户名和密码?
    如何编写jQuery插件
    函数作用域
    HTTP动词
    如何减少全局变量污染?
    mysql表大字段最大长度限制设置
    update left join 多表关联更新
  • 原文地址:https://www.cnblogs.com/lxiang/p/5201422.html
Copyright © 2011-2022 走看看