新增5個TButton按鈕,Caption分別為:
&Open
>
■
&Back
&Next
再新增1個TProgressBar,1個TListBox,1個TMediaPlayer且屬性Visible = False。
1個TOpenDialog且屬性Filter = 所有音樂格式*.wav;*.mp3;*.mid
1個TTimer
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, MPlayer, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
ProgressBar1: TProgressBar;
ListBox1: TListBox;
MediaPlayer1: TMediaPlayer;
Timer1: TTimer;
OpenDialog1: TOpenDialog;
procedure ListBox1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
procedure PlayAudio;
procedure NextTrack;
procedure BackTrack;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
ss: String;
PlayIndex: integer = 0;
implementation
{$R *.dfm}
procedure TForm1.PlayAudio;
begin
try
if ListBox1.Items.Count > 0 then //歌大於1首才可
begin
if ListBox1.ItemIndex = -1 then //未選擇就0 指第一首
ListBox1.ItemIndex := 0;
ss := ListBox1.Items.Strings[ListBox1.ItemIndex]; //取到檔名
MediaPlayer1.FileName := ss;
MediaPlayer1.Open;
MediaPlayer1.Play;
end;
except;
end;
end;
procedure TForm1.NextTrack;
begin
try
PlayIndex := ListBox1.ItemIndex;
Inc(PlayIndex); //加1
if PlayIndex = ListBox1.Items.Count then
PlayIndex := 0; //跳到第一首
ListBox1.ItemIndex := PlayIndex;
ss := ListBox1.Items.Strings[PlayIndex];
MediaPlayer1.FileName := ss;
MediaPlayer1.Open;
if MediaPlayer1.FileName = '' then
PlayAudio
else
MediaPlayer1.Resume;
except;
end;
end;
procedure TForm1.BackTrack;
begin
try
PlayIndex := ListBox1.ItemIndex;
Dec(PlayIndex); //減1
if PlayIndex < filename =" ''" filename =" ''" caption =" '">' then
begin
MediaPlayer1.Resume;
button2.caption:='';
end else
begin
MediaPlayer1.Pause;
button2.caption:='>';
end;
except;
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
try
BackTrack
except
end;
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
try
NextTrack
except
end;
end;
procedure TForm1.ListBox1Click(Sender: TObject); //連點兩下
begin
PlayAudio;
button2.caption:='';
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
if MediaPlayer1.FileName <> '' then
try
ProgressBar1.Max := MediaPlayer1.Length; //MediaPlayer1.Length音樂長度
ProgressBar1.Position := MediaPlayer1.Position; //MediaPlayer1.Position播放位置
ProgressBar1.Update;
MediaPlayer1.Update;
if MediaPlayer1.Position >= MediaPlayer1.Length then
NextTrack;
except;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
MediaPlayer1.stop; //停止播放
MediaPlayer1.FileName := ''; //名稱為''是為了跑PlayAudio
button2.caption:='>'; //按鈕圖示變
ProgressBar1.Position := 0; //手控狀態bar
end;
end.
沒有留言:
張貼留言