使用API函数mciSendString构成的媒体播放类.docx
《使用API函数mciSendString构成的媒体播放类.docx》由会员分享,可在线阅读,更多相关《使用API函数mciSendString构成的媒体播放类.docx(8页珍藏版)》请在冰豆网上搜索。
使用API函数mciSendString构成的媒体播放类
使用API函数mciSendString构成的媒体播放类
使用API函数mciSendString构成的媒体播放类.txt我自横刀向天笑,笑完我就去睡觉。
你的手机比话费还便宜。
路漫漫其修远兮,不如我们打的吧。
/*
*C#没有提供播放MP3等音频文件的类,
*要编写播放MP3等音频文件程序,
*必须使用第三方控件或类。
*本文使用API函数mciSendString,
*编写一个播放MP3等音频文件的类。
*/
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Runtime.InteropServices;
namespace使用API函数mciSendString构成的媒体播放类
{
publicpartialclassForm1:
Form
{
stringpath;
publicForm1()
{
InitializeComponent();
}
clsMCImci=null;
privatevoidbutton1_Click(objectsender,EventArgse)
{
mci=newclsMCI();
mci.FileName=path;
this.progressBar1.Maximum=mci.Duration;
mci.play();
this.timer1.Enabled=true;
}
privatevoidbutton2_Click(objectsender,EventArgse)
{
if(mci==null)
{
}
else
{
this.timer1.Enabled=false;
mci.StopT();
}
}
privatevoidbutton3_Click(objectsender,EventArgse)
{
OpenFileDialogop=newOpenFileDialog();
if(op.ShowDialog()==DialogResult.OK)
{
path=op.FileName;
}
}
privatevoidForm1_Load(objectsender,EventArgse)
{
}
privatevoidtimer1_Tick(objectsender,EventArgse)
{
this.progressBar1.Value=mci.CurrentPosition;
if(mci.CurrentPosition==mci.Duration)
{
this.timer1.Enabled=false;
}
}
}
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;
durLength=durLength.Substring(0,durLength.IndexOf('\0'));
return(int)(Convert.ToDouble(durLength)/1000f);
}
}
///
///当前时间
///
publicintCurrentPosition
{
get
{
durLength="";
durLength=durLength.PadLeft(128,Convert.ToChar(""));
APIClass.mciSendString("statusmediaposition",durLength,durLength.Length,0);
durLength=durLength.Substring(0,durLength.IndexOf('\0'));
mc.iPos=(int)(Convert.ToDouble(durLength)/1000f);
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
);
}
}