【C#获取音频文件的播放时间】
2014-09-18 09:59
639 查看
[DllImport("Kernel32", CharSet = CharSet.Auto)]
static extern Int32 GetShortPathName(String path, StringBuilder shortPath, Int32 shortPathLength);
[DllImport("winmm.dll")]
public static extern int mciSendString(string m_strCmd, StringBuilder m_strReceive, int m_v1, int m_v2);
private string getasfTime(string filePath)
{
StringBuilder shortpath = new StringBuilder(80);
GetShortPathName(filePath, shortpath, shortpath.Capacity);
string name = shortpath.ToString();
StringBuilder buf = new StringBuilder(80);
mciSendString("close all", buf, buf.Capacity, 0);
mciSendString("open " + name + " alias media", buf, buf.Capacity, 0);
mciSendString("status media length", buf, buf.Capacity, 0);
TimeSpan ts = new TimeSpan(0, 0, 0, 0, (int)Convert.ToDouble(buf.ToString().Trim()));
return ts.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
this.label1.Text = getasfTime("F:\\视频\\游戏视频-贼法竞技场2v2 2200+.avi");
}
注:该程序段能获取的音频或者视频的格式由 [DllImport("winmm.dll")]中的winmm。dll决定。当然可以导入其他dll文件来支持其他格式。但其中函数也随之变化。
添加引用SHELL32.DLL
public static class GetVideoLength
{
public static string GetMediaTimeLen(string path)
{
try
{
Shell32.Shell shell = new Shell32.Shell();
//文件路径
Shell32.Folder folder = shell.NameSpace(path.Substring(0, path.LastIndexOf("\\")));
//文件名称
Shell32.FolderItem folderitem = folder.ParseName(path.Substring(path.LastIndexOf("\\") + 2));
if (Environment.OSVersion.Version.Major >= 6)
{
return folder.GetDetailsOf(folderitem, 27);
}
else
{
return folder.GetDetailsOf(folderitem, 21);
}
}
catch (Exception ex) { return null; }
}
public static int GetMediaTimeLenSecond(string path)
{
try
{
Shell32.Shell shell = new Shell32.Shell();
//文件路径
Shell32.Folder folder = shell.NameSpace(path.Substring(0, path.LastIndexOf("\\")));
//文件名称
Shell32.FolderItem folderitem = folder.ParseName(path.Substring(path.LastIndexOf("\\") + 2));
string len;
if (Environment.OSVersion.Version.Major >= 6)
{
len = folder.GetDetailsOf(folderitem, 27);
}
else
{
len = folder.GetDetailsOf(folderitem, 21);
}
string[] str = len.Split(new char[] { ':' });
int sum = 0;
sum = int.Parse(str[0]) * 60 * 60 + int.Parse(str[1]) * 60 + int.Parse(str[2]);
return sum;
}
catch (Exception ex) { return 0; }
}
}
static extern Int32 GetShortPathName(String path, StringBuilder shortPath, Int32 shortPathLength);
[DllImport("winmm.dll")]
public static extern int mciSendString(string m_strCmd, StringBuilder m_strReceive, int m_v1, int m_v2);
private string getasfTime(string filePath)
{
StringBuilder shortpath = new StringBuilder(80);
GetShortPathName(filePath, shortpath, shortpath.Capacity);
string name = shortpath.ToString();
StringBuilder buf = new StringBuilder(80);
mciSendString("close all", buf, buf.Capacity, 0);
mciSendString("open " + name + " alias media", buf, buf.Capacity, 0);
mciSendString("status media length", buf, buf.Capacity, 0);
TimeSpan ts = new TimeSpan(0, 0, 0, 0, (int)Convert.ToDouble(buf.ToString().Trim()));
return ts.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
this.label1.Text = getasfTime("F:\\视频\\游戏视频-贼法竞技场2v2 2200+.avi");
}
注:该程序段能获取的音频或者视频的格式由 [DllImport("winmm.dll")]中的winmm。dll决定。当然可以导入其他dll文件来支持其他格式。但其中函数也随之变化。
添加引用SHELL32.DLL
public static class GetVideoLength
{
public static string GetMediaTimeLen(string path)
{
try
{
Shell32.Shell shell = new Shell32.Shell();
//文件路径
Shell32.Folder folder = shell.NameSpace(path.Substring(0, path.LastIndexOf("\\")));
//文件名称
Shell32.FolderItem folderitem = folder.ParseName(path.Substring(path.LastIndexOf("\\") + 2));
if (Environment.OSVersion.Version.Major >= 6)
{
return folder.GetDetailsOf(folderitem, 27);
}
else
{
return folder.GetDetailsOf(folderitem, 21);
}
}
catch (Exception ex) { return null; }
}
public static int GetMediaTimeLenSecond(string path)
{
try
{
Shell32.Shell shell = new Shell32.Shell();
//文件路径
Shell32.Folder folder = shell.NameSpace(path.Substring(0, path.LastIndexOf("\\")));
//文件名称
Shell32.FolderItem folderitem = folder.ParseName(path.Substring(path.LastIndexOf("\\") + 2));
string len;
if (Environment.OSVersion.Version.Major >= 6)
{
len = folder.GetDetailsOf(folderitem, 27);
}
else
{
len = folder.GetDetailsOf(folderitem, 21);
}
string[] str = len.Split(new char[] { ':' });
int sum = 0;
sum = int.Parse(str[0]) * 60 * 60 + int.Parse(str[1]) * 60 + int.Parse(str[2]);
return sum;
}
catch (Exception ex) { return 0; }
}
}
相关文章推荐
- C#获取音频文件(MP3等)播放时间长度的几种方式
- C#获取音频文件(MP3等)播放时间长度的几种方式
- 音频文件播放时间的获取
- IOS MPMoviePlayerViewController 获取当前音频文件播放时间点
- C#获取视频文件的播放时间
- C#获取web上传音频文件怎么得到文件时间长度
- C#获取 Flv视频文件播放时间长度等信息
- c#获取远程文件更新时间
- C#.net获取指定文件夹下的所有jpg文件,并按文件创建时间排序
- C#使用Windows Media Player播放音频文件
- C#使用SoundPlayer播放WAV音频文件
- 转:c#播放音频文件
- C#获取视频文件播放长度
- C#里面用API完成MP3等音频文件地播放类
- (转)C#中用API实现MP3等音频文件的播放类
- C# 获取文件大小,创建时间,文件信息,FileInfo类的属性表
- [导入]C#中用API实现MP3等音频文件的播放类
- C#中用API实现MP3等音频文件的播放类(转)
- 使用API函数播放MP3等音频文件的类设计(C#)
- 使用API函数播放MP3等音频文件的类设计(C#)