这个是从别人的blog是看到的.
经过我的验证,完全可以使用,而且不仅支持mp3,还支持wma,wmv.播放wmv的话会自动弹出一个视频播放小窗口.
代码:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Runtime.InteropServices;
using
System.IO;
using
System.Windows.Forms;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
namespace
winformFlash
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**////<summary>
///用Win32Api实现的媒体播放类.支持Mp3,wma,wmv
///</summary>
classMp3
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
publicMp3()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
}
//定义API函数使用的字符串变量
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=260)]
privatestringName="";
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=128)]
privatestringdurLength="";
[MarshalAs(UnmanagedType.LPTStr,SizeConst=128)]
privatestringTemStr="";
intilong;
//定义播放状态枚举变量
publicenumState
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
mPlaying=1,
mPuase=2,
mStop=3
};
//结构变量
publicstructstructMCI
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
publicboolbMut;
publicintiDur;
publicintiPos;
publicintiVol;
publicintiBal;
publicstringiName;
publicStatestate;
};
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
publicstructMCImc=newstructMCI();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//取得播放文件属性
publicstringFileName
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
get
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
returnmc.iName;
}
set
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//ASCIIEncodingasc=newASCIIEncoding();
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
TemStr="";
TemStr=TemStr.PadLeft(127,Convert.ToChar(""));
Name=Name.PadLeft(260,Convert.ToChar(""));
mc.iName=value;
ilong=APIClass.GetShortPathName(mc.iName,Name,Name.Length);
Name=GetCurrPath(Name);
//Name="open"+Convert.ToChar(34)+Name+Convert.ToChar(34)+"aliasmedia";
Name="open"+Convert.ToChar(34)+Name+Convert.ToChar(34)+"aliasmedia";
ilong=APIClass.mciSendString("closeall",TemStr,TemStr.Length,0);
ilong=APIClass.mciSendString(Name,TemStr,TemStr.Length,0);
ilong=APIClass.mciSendString("setmediatimeformatmilliseconds",TemStr,TemStr.Length,0);
mc.state=State.mStop;
}
catch
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MessageBox.Show("出错错误!");
}
}
}
//播放
publicvoidplay()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
TemStr="";
TemStr=TemStr.PadLeft(127,Convert.ToChar(""));
APIClass.mciSendString("playmedia",TemStr,TemStr.Length,0);
mc.state=State.mPlaying;
}
//停止
publicvoidStopT()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
TemStr="";
TemStr=TemStr.PadLeft(128,Convert.ToChar(""));
ilong=APIClass.mciSendString("closemedia",TemStr,128,0);
ilong=APIClass.mciSendString("closeall",TemStr,128,0);
mc.state=State.mStop;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
publicvoidPuase()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
TemStr="";
TemStr=TemStr.PadLeft(128,Convert.ToChar(""));
ilong=APIClass.mciSendString("pausemedia",TemStr,TemStr.Length,0);
mc.state=State.mPuase;
}
privatestringGetCurrPath(stringname)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(name.Length<1)return"";
name=name.Trim();
name=name.Substring(0,name.Length-1);
returnname;
}
//总时间
publicintDuration
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
get
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
durLength="";
durLength=durLength.PadLeft(128,Convert.ToChar(""));
APIClass.mciSendString("statusmedialength",durLength,durLength.Length,0);
durLength=durLength.Trim();
if(durLength=="")return0;
return(int)(Convert.ToDouble(durLength)/1000f);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//当前时间
publicintCurrentPosition
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
get
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
durLength="";
durLength=durLength.PadLeft(128,Convert.ToChar(""));
APIClass.mciSendString("statusmediaposition",durLength,durLength.Length,0);
mc.iPos=(int)(Convert.ToDouble(durLength)/1000f);
returnmc.iPos;
}
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
public
class
APIClass
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
[DllImport("kernel32.dll",CharSet=CharSet.Auto)]
publicstaticexternintGetShortPathName(
stringlpszLongPath,
stringshortFile,
intcchBuffer
);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
[DllImport("winmm.dll",EntryPoint="mciSendString",CharSet=CharSet.Auto)]
publicstaticexternintmciSendString(
stringlpstrCommand,
stringlpstrReturnString,
intuReturnLength,
inthwndCallback
);
}
代码下载: Mp3ForCSharp.zip
代码来源:http://myliulongtao.icode.csdn.net/post/2007/06/13/54135