Dzwebs.Net

撰写电脑技术杂文十余年

C#代码获取音频音乐文件的播放总时间

Admin | 2014-10-3 9:12:03 | 被阅次数 | 6270

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

  代码如下,功能为,获取音乐文件的播放总时长。

  string   file = @"C:\我的音乐.mp3" ;

  ShellClass sh = new   ShellClass();

  Folder dir = sh.NameSpace(Path.GetDirectoryName(file));

  FolderItem item = dir.ParseName(Path.GetFileName(file));

  string   str = dir.GetDetailsOf(item, 27) ; // 获取歌曲时长。

  MessageBox.Show(str);

  / 如果执行过程中,出现是错误提示,通过 Shell32 的 Embed Interop Types 属性设置为 False 即可;

  知识拓展:C# 获得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; 
  } 


该杂文来自: 网站开发杂文

上一篇:C#代码实现窗体改变大小控件自适应

下一篇:IE访问网址,网址自动多出很多字符如何解决

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计