C#获取音频文件(MP3等)播放时间长度的几种方式

时间:2021-02-23 19:43:28

先介绍3种,分别是API方式,读取文件信息方式,使用DirectX中的AudioVideoPlayback方式。

1.API方式

通过winmm.dll中mciSendString多媒体API函数来获得音频文件信息。

主要代码:

using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
	openFileDialog.Filter = "MP3文件(*.mp3)|*.mp3";
	if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
	{
		string fileName = openFileDialog.FileName;
		TemStr = "";
		MP3Name = "";
		TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
		MP3Name = MP3Name.PadLeft(260, Convert.ToChar(" "));
		ilong = APIClass.GetShortPathName(fileName, MP3Name, MP3Name.Length);
		MP3Name = GetCurrPath(MP3Name);
		MP3Name = "Open \"" + MP3Name + "\" type mpegvideo alias mysong";
		ilong = APIClass.mciSendString(MP3Name, "", 0, 0);
		durLength = "";
		durLength = durLength.PadLeft(128, Convert.ToChar(" "));
		ilong = APIClass.mciSendString("set mysong time format milliseconds", TemStr, TemStr.Length, 0);
		ilong = APIClass.mciSendString("status mysong length", durLength, durLength.Length, 0);
		durLength = durLength.Trim();
		if (durLength == "")
		{
			btn.Content = 0;
		}
		else
		{
			int s_sum = (int)(Convert.ToInt32(durLength) / 1000);
			int h = (int)(s_sum / 3600);
			int m = (int)(s_sum / 60) - h * 60;
			int s = (int)s_sum % 60;
			btn.Content = string.Format("{0}:{1:d2}:{2:d2}", h, m, s);
		}
	}
}

2.读取文件信息方式

需要引用COM组件的Microsoft Shell Controls And Automation

主要代码:

using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
	if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
	{
		string strFileName = openFileDialog.FileName;
		string dirName = System.IO.Path.GetDirectoryName(strFileName);
		string SongName = System.IO.Path.GetFileName(strFileName);//获得歌曲名称
		FileInfo fInfo = new FileInfo(strFileName);
		ShellClass sh = new ShellClass();
		Folder dir = sh.NameSpace(dirName);
		FolderItem item = dir.ParseName(SongName);
		btn.Content = Regex.Match(dir.GetDetailsOf(item, -1), "\\d:\\d{2}:\\d{2}").Value;//获取歌曲时间
	}
}

3.使用DirectX中的AudioVideoPlayback方式

引用Microsoft.DirectX.AudioVideoPlayback;

注意:64位系统需将项目生成为x86应用程序。

如为.NET4.0平台调试,将app.config修改为:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <!--<supportedRuntime version="v2.0.50727"/>-->
    <startup useLegacyV2RuntimeActivationPolicy="true">
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
  </startup>
</configuration>

另外:调试中DirectX报错,如需过滤,将菜单 调试->异常->Managed Debugging Assistants中勾去掉即可。

主要代码:

using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
	if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
	{
		string strFileName = openFileDialog.FileName;
		Audio ourAudio1 = new Audio(strFileName);
		int h = (int)(ourAudio1.Duration / 3600);
		int m = (int)(ourAudio1.Duration / 60) - h * 60;
		int s = (int)ourAudio1.Duration % 60;
		btn.Content = string.Format("{0}:{1:d2}:{2:d2}", h, m, s);
		//ourAudio1.Play();
	}
}

代码


作者:FoolRabbit
出处:http://blog.csdn.net/rabbitsoft_1987
欢迎任何形式的转载,未经作者同意,请保留此段声明!