1.前言
---- mp3因其较高的压缩率,较好的音质,成为越来越多的使用者的喜爱,用自己编制的mp3播放器听mp3音乐一定很有乐趣.我以下介绍一个用delphi编制的简单mp3播放器,因为mp3音乐的解码比较复杂,普通的程序员很难编制其解码程序,但不要紧,我们可以用别人写好的解码程序来完成我们的程序.
---- 2.设计构思
---- 我们使用mpegdll.dll来解码(很多国产的mp3播放器都用它),mpegdll.dll的作者提供了它的delphi的控件,在各大delphi网站都有下载,或在作者的主页http://www.ig.com.ua/wabbit/programs/workshop.html下载之后,我们安装控件(必须delphi3.0以上),在component中选择install component,安装控件包中的mpegplayer.dcu,安装完成后在控件栏里多出一个add-ons来,控件就在里面.
---- 介绍一下用到的参数
mpeg.streamname 所要播放的文件名 mpeg.length 文件程度 mpeg.bitrate 压缩位率 mpeg.frequency 压缩频率 mpeg.layer 压缩层次 mpeg.mode 压缩模式 mpeg.pathtodll dll文件的路径
---- 新建一个from,放入6个label,5个button,1个checkbox,1个mpeg,1个time,1个trackbar,1个opendialog控件调整为适当的布局,点击opendialog1使*.mp3成为打开文件的后缀名.调整5个button,把其属性name改为 openbutton,pausebutton,stopbutton,playbutton和exitbutton,并把caption改为播放,暂停,停止,打开,和退出. 使3个label的caption成为,压缩位率,压缩频率,压缩层次,调整其他3个label使label4对应label1,label5对应label2,label6对应label3 caption为空,autosize为true,trackbar的tickstyle设为tsNone,chickbox的caption设为循环播放,timer的ontimer设为timer1timer ---- 3.程序清单如下
unit Unit1;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, MPEGPlayer,Gauges, ComCtrls,Mask, StdCtrls;
type TForm1 = class(TForm) Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; Label5: TLabel; openButton: TButton; playButton: TButton; pauseButton: TButton; stopButton: TButton; exitButton: TButton; CheckBox1: TCheckBox; TrackBar1: TTrackBar; MPEG: TMPEGPlayer; Timer1: TTimer; OpenDialog1: TOpenDialog; Label6: TLabel; procedure exitButtonClick(Sender: TObject); procedure openButtonClick(Sender: TObject); procedure playButtonClick(Sender: TObject); procedure pauseButtonClick(Sender: TObject); procedure stopButtonClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure TrackBar1Change(Sender: TObject); procedure Timer1Timer(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private declarations } dontseek:boolean; public { Public declarations } end;
var Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.exitButtonClick(Sender: TObject); begin close; end;
procedure TForm1.openButtonClick(Sender: TObject); var s:string; begin if not opendialog1.execute then exit; mpeg.autoplay:=false; mpeg.streamname:=opendialog1.filename; mpeg.open; trackbar1.Max:=mpeg.length; trackbar1.position:=0; str(trackbar1.max,s); playbutton.Enabled:=true; stopbutton.enabled:=true; pausebutton.Enabled:=true; end;
procedure TForm1.playButtonClick(Sender: TObject); var a,b,c:string; begin playbutton.Enabled:=false; openbutton.Enabled:=false; mpeg.Play; str(mpeg.Bitrate,a); label4.Caption:=a; str(mpeg.Frequency,b); label5.caption:=b; str(mpeg.layer,c); label6.caption:=c; end; procedure TForm1.pauseButtonClick(Sender: TObject); begin mpeg.paused:=not (mpeg.Paused); end;
procedure TForm1.stopButtonClick(Sender: TObject); begin if mpeg.mode>2 then begin mpeg.stop; mpeg.close; end; trackbar1.Position:=0; openbutton.Enabled:=true; playbutton.enabled:=false; stopbutton.enabled:=false; pausebutton.enabled:=false; label4.Caption:=''; label5.Caption:=''; label6.Caption:=''; end;
procedure TForm1.FormCreate(Sender: TObject); begin mpeg.pathtodll:=''; //存放dll闻家的路径本例程序和dll在同一路径下 //dll一定要放在程序同一目录,否则不能编译 mpeg.init; playbutton.Enabled :=false; pausebutton.enabled:=false; stopbutton.enabled:=false; end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin mpeg.deinit; end; procedure TForm1.TrackBar1Change(Sender: TObject); begin if not(dontseek) then if (mpeg.mode=3)or((mpeg.mode=4)) then mpeg.CurrentPosition:=trackbar1.Position; end; procedure TForm1.Timer1Timer(Sender: TObject); var a,b,c:string; begin if (mpeg.mode=3) then begin dontseek:=true; trackbar1.Position:=mpeg.CurrentPosition; dontseek:=false;
end; if mpeg.PlayStopped then begin if checkbox1.Checked then begin mpeg.Play; playbutton.Enabled:=false; openbutton.Enabled:=false; str(mpeg.Bitrate,a); label4.Caption:=a; str(mpeg.Frequency,b); label5.caption:=b; str(mpeg.layer,c); label6.caption:=c; end else begin mpeg.close; trackbar1.Position:=0; openbutton.Enabled:=true; playbutton.Enabled :=false; pausebutton.enabled:=false; stopbutton.enabled:=false; label4.Caption:=''; label5.Caption:=''; label6.Caption:=''; end; end; end; end. ---- 以上是一个简单的mp3播放器的例程,mpegplayer是一个强大的控件可以实现很多功能,如多曲播放,请大家自己研究把,愿大家都能做出很好的mp3播放器 |