首页 > 开发 > 综合 > 正文

使用API函数播放MP3等音频文件的类设计(C#)

2024-07-21 02:18:03
字体:
来源:转载
供稿:网友
c#没有提供播放mp3等音频文件的类,要编写播放mp3等音频文件程序,必须使用第三方控件或类。本文使用api函数mcisendstring,编写一个播放mp3等音频文件的类。 具体源码如下:
一、使用api函数mcisendstring构成的媒体播放类。
using system;
using system.runtime.interopservices;
using system.text;
using system.io ;

namespace clsmciplay
{
/// <summary>
/// clsmci 的摘要说明。
/// </summary>
public class clsmci
{
public clsmci()
{
//
// todo: 在此处添加构造函数逻辑
//
}

//定义api函数使用的字符串变量
[marshalas(unmanagedtype.byvaltstr,sizeconst=260)]
private string name = "" ;
[marshalas(unmanagedtype.byvaltstr,sizeconst=128)]
private string durlength = "" ;
[marshalas(unmanagedtype.lptstr,sizeconst=128)]
private string temstr ="";
int ilong;
//定义播放状态枚举变量
public enum state
{
mplaying = 1,
mpuase = 2,
mstop = 3
};
//结构变量
public struct structmci
{
public bool bmut;
public int idur;
public int ipos;
public int ivol;
public int ibal;
public string iname;
public state state;
};

public structmci mc =new structmci() ;

//取得播放文件属性
public string filename
{
get
{
return mc.iname;
}

set
{
//asciiencoding asc = new asciiencoding();
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) + " alias media";
name = "open " + convert.tochar(34) + name + convert.tochar(34) + " alias media";
ilong = apiclass.mcisendstring("close all", temstr, temstr.length , 0);
ilong = apiclass.mcisendstring( name, temstr, temstr.length, 0);
ilong = apiclass.mcisendstring("set media time format milliseconds", temstr, temstr.length , 0);
mc.state = state.mstop;
}
catch
{
messagebox.show("出错错误!");
}
}
}
//播放
public void play()
{
temstr = "";
temstr = temstr.padleft(127,convert.tochar(" "));
apiclass.mcisendstring("play media", temstr, temstr.length , 0);
mc.state = state.mplaying ;
}
//停止
public void stopt()
{
temstr = "";
temstr = temstr.padleft(128,convert.tochar(" "));
ilong = apiclass.mcisendstring("close media", temstr, 128, 0);
ilong = apiclass.mcisendstring("close all", temstr, 128, 0);
mc.state = state.mstop ;

}

public void puase()
{
temstr = "";
temstr = temstr.padleft(128,convert.tochar(" "));
ilong = apiclass.mcisendstring("pause media", temstr, temstr.length, 0);
mc.state = state.mpuase ;
}
private string getcurrpath(string name)
{
if(name.length <1) return "";
name = name.trim();
name = name.substring(0,name.length-1);
return name;
}
//总时间
public int duration
{
get
{
durlength = "";
durlength = durlength.padleft(128,convert.tochar(" ")) ;
apiclass.mcisendstring("status media length", durlength, durlength.length, 0);
durlength = durlength.trim();
if(durlength == "") return 0;
return (int)(convert.todouble(durlength) / 1000f);
}
}

//当前时间
public int currentposition
{
get
{
durlength = "";
durlength = durlength.padleft(128,convert.tochar(" ")) ;
apiclass.mcisendstring("status media position", durlength, durlength.length, 0);
mc.ipos = (int)(convert.todouble(durlength) / 1000f);
return mc.ipos;
}
}



}

public class apiclass
{

[dllimport("kernel32.dll", charset = charset.auto)]
public static extern int getshortpathname (
string lpszlongpath,
string shortfile,
int cchbuffer
);

[dllimport("winmm.dll", entrypoint="mcisendstring", charset = charset.auto)]
public static extern int mcisendstring (
string lpstrcommand,
string lpstrreturnstring,
int ureturnlength,
int hwndcallback
);

}

}

二、用于测试媒体播放类的简单代码:
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.runtime.interopservices;
using system.text;
using system.io ;
using clsmciplay;

namespace mciplay
{
/// <summary>
/// form1 的摘要说明。
/// </summary>
public class form1 : system.windows.forms.form
{
private system.componentmodel.icontainer components;
private system.windows.forms.timer timer1;
private system.windows.forms.button play;
private system.windows.forms.button stop;
private system.windows.forms.button puase;
private system.windows.forms.label playfilename;
private system.windows.forms.label duration;
private system.windows.forms.label currentposition;
private system.windows.forms.openfiledialog openfiledialog1;
private system.windows.forms.button browserfile;
clsmci mp = new clsmci();


public form1()
{
//
// windows 窗体设计器支持所必需的
//
initializecomponent();

//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}

#region windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.components = new system.componentmodel.container();
this.play = new system.windows.forms.button();
this.playfilename = new system.windows.forms.label();
this.duration = new system.windows.forms.label();
this.stop = new system.windows.forms.button();
this.puase = new system.windows.forms.button();
this.currentposition = new system.windows.forms.label();
this.timer1 = new system.windows.forms.timer(this.components);
this.openfiledialog1 = new system.windows.forms.openfiledialog();
this.browserfile = new system.windows.forms.button();
this.suspendlayout();
//
// play
//
this.play.location = new system.drawing.point(102, 243);
this.play.name = "play";
this.play.size = new system.drawing.size(78, 24);
this.play.tabindex = 0;
this.play.text = "play";
this.play.click += new system.eventhandler(this.play_click);
//
// playfilename
//
this.playfilename.autosize = true;
this.playfilename.location = new system.drawing.point(12, 15);
this.playfilename.name = "playfilename";
this.playfilename.size = new system.drawing.size(0, 17);
this.playfilename.tabindex = 1;
//
// duration
//
this.duration.autosize = true;
this.duration.location = new system.drawing.point(15, 51);
this.duration.name = "duration";
this.duration.size = new system.drawing.size(0, 17);
this.duration.tabindex = 2;
//
// stop
//
this.stop.location = new system.drawing.point(282, 243);
this.stop.name = "stop";
this.stop.size = new system.drawing.size(81, 24);
this.stop.tabindex = 3;
this.stop.text = "stop";
this.stop.click += new system.eventhandler(this.stop_click);
//
// puase
//
this.puase.location = new system.drawing.point(198, 243);
this.puase.name = "puase";
this.puase.size = new system.drawing.size(72, 24);
this.puase.tabindex = 4;
this.puase.text = "puase";
this.puase.click += new system.eventhandler(this.puase_click);
//
// currentposition
//
this.currentposition.autosize = true;
this.currentposition.location = new system.drawing.point(15, 87);
this.currentposition.name = "currentposition";
this.currentposition.size = new system.drawing.size(0, 17);
this.currentposition.tabindex = 5;
//
// timer1
//
this.timer1.enabled = true;
this.timer1.interval = 1000;
this.timer1.tick += new system.eventhandler(this.timer1_tick);
//
// browserfile
//
this.browserfile.location = new system.drawing.point(312, 165);
this.browserfile.name = "browserfile";
this.browserfile.size = new system.drawing.size(87, 24);
this.browserfile.tabindex = 6;
this.browserfile.text = "selectfile";
this.browserfile.click += new system.eventhandler(this.browserfile_click);
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(433, 287);
this.controls.add(this.browserfile);
this.controls.add(this.currentposition);
this.controls.add(this.puase);
this.controls.add(this.stop);
this.controls.add(this.duration);
this.controls.add(this.playfilename);
this.controls.add(this.play);
this.name = "form1";
this.text = "form1";
this.resumelayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main()
{
application.run(new form1());
}

//选择mp3文件播放
private void play_click(object sender, system.eventargs e)
{
try
{
mp.play();
}
catch
{
messagebox.show("出错错误!");
}
}

//暂停播放
private void puase_click(object sender, system.eventargs e)
{
try
{
mp.puase();
}
catch
{
messagebox.show("出错错误!");
}

}

//停止播放
private void stop_click(object sender, system.eventargs e)
{
try
{
mp.stopt();
}
catch
{
messagebox.show("出错错误!");
}
}

//每秒显示一次播放进度
private void timer1_tick(object sender, system.eventargs e)
{
currentposition.text = mp.currentposition.tostring();
}

//浏览文件
private void browserfile_click(object sender, system.eventargs e)
{
try
{
openfiledialog1.filter = "*.mp3|*.mp3";
openfiledialog1.filename = "";
if(openfiledialog1.showdialog() == dialogresult.ok)
{
mp.filename = openfiledialog1.filename ;
playfilename.text = openfiledialog1.filename ;
duration.text = mp.duration.tostring() ;
}
}
catch
{
messagebox.show("出错错误!");
}
}

}

}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表