C#播放MP3的类

C#没有提供播放MP3等音频文件的类,要编写播放MP3等音频文件程序,必须使用第三方控件或类。本文使用API函数mciSendString,编写一个播放MP3等音频文件的类。

  具体源码如下:

  一、使用API函数mciSendString构成的媒体播放类。

<shapetype id="_x0000_t75" path="m@4@5l@4@11@9@11@9@5xe" o:spt="75" coordsize="21600,21600" stroked="f" filled="f" o:preferrelative="t"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock v:ext="edit" aspectratio="t"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 12pt; HEIGHT: 12pt" alt="程序代码" type="#_x0000_t75"><imagedata o:href="http://www.113317.com/blog/images/code.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>程序代码

usingSystem;
usingSystem.Runtime.InteropServices;
usingSystem.Text;
usingSystem.IO;
namespaceclsMCIPlay
{
 ///<summary>
 ///clsMci的摘要说明。
 ///</summary>
 publicclassclsMCI
 {
  publicclsMCI()
  {
   //
   //TODO:在此处添加构造函数逻辑
   //
  }

  //定义API函数使用的字符串变量
  [MarshalAs(UnmanagedType.ByValTStr,SizeConst=260)]
  privatestringName="";
  [MarshalAs(UnmanagedType.ByValTStr,SizeConst=128)]
  privatestringdurLength="";
  [MarshalAs(UnmanagedType.LPTStr,SizeConst=128)]
  privatestringTemStr="";
  intilong;
  //定义播放状态枚举变量
  publicenumState
  {
   mPlaying=1,
   mPuase=2,
   mStop=3
  };
  //结构变量
  publicstructstructMCI
  {
   publicboolbMut;
   publicintiDur;
   publicintiPos;
   publicintiVol;
   publicintiBal;
   publicstringiName;
   publicStatestate;
  };

  publicstructMCImc=newstructMCI();

  //取得播放文件属性
  publicstringFileName
  {
   get
   {
    returnmc.iName;
   }
   set
   {
    //ASCIIEncodingasc=newASCIIEncoding();
    try
    {
     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
    {
     MessageBox.Show("出错错误!");
    }
   }
  }
  //播放
  publicvoidplay()
  {
   TemStr="";
   TemStr=TemStr.PadLeft(127,Convert.ToChar(""));
   APIClass.mciSendString("playmedia",TemStr,TemStr.Length,0);
   mc.state=State.mPlaying;
  }
  //停止
  publicvoidStopT()
  {
   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;
  }

  publicvoidPuase()
  {
   TemStr="";
   TemStr=TemStr.PadLeft(128,Convert.ToChar(""));
   ilong=APIClass.mciSendString("pausemedia",TemStr,TemStr.Length,0);
   mc.state=State.mPuase;
  }
  privatestringGetCurrPath(stringname)
  {
   if(name.Length<1)return"";
   name=name.Trim();
   name=name.Substring(0,name.Length-1);
   returnname;
  }
  //总时间
  publicintDuration
  {
   get
   {
    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)/<chmetcnv w:st="on" unitname="F" sourcevalue="1000" hasspace="False" negative="False" numbertype="1" tcsc="0">1000f</chmetcnv>);
   }
  }

  //当前时间
  publicintCurrentPosition
  {
   get
   {
    durLength="";
    durLength=durLength.PadLeft(128,Convert.ToChar(""));
    APIClass.mciSendString("statusmediaposition",durLength,durLength.Length,0);
    mc.iPos=(int)(Convert.ToDouble(durLength)/<chmetcnv w:st="on" unitname="F" sourcevalue="1000" hasspace="False" negative="False" numbertype="1" tcsc="0">1000f</chmetcnv>);
    returnmc.iPos;
   }
  }
 }

 publicclassAPIClass
 {
  [DllImport("kernel32.dll",CharSet=CharSet.Auto)]
  publicstaticexternintGetShortPathName(
   stringlpszLongPath,
   stringshortFile,
   intcchBuffer
  );

  [DllImport("winmm.dll",EntryPoint="mciSendString",CharSet=CharSet.Auto)]
  publicstaticexternintmciSendString(
   stringlpstrCommand,
   stringlpstrReturnString,
   intuReturnLength,
   inthwndCallback
  );
 }
}

你可能感兴趣的:(C++,c,F#,ext,C#)