求一个播放列表的思路,
播放tstrings里面的路径
循环播放 单曲循环 暂停/暂停恢复 上一曲 下一曲最好不用Timer控件好麻烦!!!!有现成的能实现以上功能的函数另外加分.非常感谢. 非常之郁闷 弄了一晚上了.救救我吧
播放tstrings里面的路径
循环播放 单曲循环 暂停/暂停恢复 上一曲 下一曲最好不用Timer控件好麻烦!!!!有现成的能实现以上功能的函数另外加分.非常感谢. 非常之郁闷 弄了一晚上了.救救我吧
具体安装办法参见:http://topic.csdn.net/t/20030908/21/2237838.html研究了这个我想“循环播放”就不必动脑筋了
嗯,其实用MediaPlayer也不太难。写了一个代码,你试一下。代码还有问题,不过现在没时间改正了,你看看吧。如果有时间,我再改正一下。
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, MPlayer, ExtCtrls;type
TForm1 = class(TForm)
ListBox1: TListBox;
MediaPlayer1: TMediaPlayer;
Button1: TButton;
Button2: TButton;
Button3: TButton;
CheckBox1: TCheckBox;
Button4: TButton;
Button5: TButton;
OpenDialog1: TOpenDialog;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure MediaPlayer1Notify(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
FP: integer;
procedure PlayLists(Stp: integer);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.PlayLists(Stp: integer);
var
F : string;
begin
if Self.ListBox1.Items.Count<1 then
Exit;
if FP < 0 then
FP := Self.ListBox1.Count-1;
if FP > Self.ListBox1.Count-1 then
FP := 0;
Self.ListBox1.ItemIndex := FP;
F := Self.ListBox1.Items[FP];
Self.Caption := Format('No.%d:正在播放%s',[Self.ListBox1.ItemIndex, F]);
FP := FP+Stp;
Self.MediaPlayer1.FileName := F;
Self.MediaPlayer1.Open;
Self.MediaPlayer1.Play;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Self.Button1.Caption := '添加列表';
Self.Button2.Caption := '播放';
Self.Button3.Caption := '上一曲';
Self.Button4.Caption := '下一曲';
Self.Button5.Caption := '清空列表';
Self.CheckBox1.Caption := '循环播放';
{以下是为了测试方便}
Self.ListBox1.Items.Add('C:\WINDOWS\Media\Windows XP 启动.wav');
Self.ListBox1.Items.Add('C:\WINDOWS\Media\Windows XP 关机.wav');
Self.ListBox1.Items.Add('C:\WINDOWS\Media\Windows XP 登录音.wav');
Self.ListBox1.Items.Add('C:\WINDOWS\Media\Windows XP 注销音.wav');
Self.ListBox1.Items.Add('C:\WINDOWS\Media\tada.wav');
end;procedure TForm1.Button1Click(Sender: TObject);
begin
if Self.OpenDialog1.Execute then
Self.ListBox1.Items.Add(Self.OpenDialog1.FileName);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
with TButton(Sender) do begin
case Tag of
0: begin
Self.MediaPlayer1.OnNotify := Self.MediaPlayer1Notify;
Self.PlayLists(1);
Caption := '暂停';
end;
1: begin
Self.MediaPlayer1.OnNotify := nil;
Self.MediaPlayer1.Stop;
Caption := '播放';
end;
end;
Tag := Byte(not Boolean(Tag));
end;
end;procedure TForm1.MediaPlayer1Notify(Sender: TObject);
begin
if Self.CheckBox1.Checked then
if Self.MediaPlayer1.NotifyValue = nvSuccessful then
Self.PlayLists(1);
end;procedure TForm1.Button3Click(Sender: TObject);
begin
Self.PlayLists(-1);
end;procedure TForm1.Button4Click(Sender: TObject);
begin
Self.PlayLists(1);
end;end.
OK, 5楼代码基本修改好了, 如下.
当然, 代码还可以再优化, 你也可以再加新的功能, 关键的代码已经写出了, 楼主可以举一反三
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, MPlayer, ExtCtrls;type
TForm1 = class(TForm)
ListBox1: TListBox;
MediaPlayer1: TMediaPlayer;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
CheckBox1: TCheckBox;
OpenDialog1: TOpenDialog;
procedure MediaPlayer1Notify(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
FP: integer;
procedure PlayLists(Stp: integer);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.PlayLists(Stp: integer);
{Stp只可取值为0,1,-1三者之一}
var
F : string;
begin
if Self.ListBox1.Items.Count<1 then Exit;
if Stp<>0 then Inc(FP, Stp);
if FP < 0 then FP := Self.ListBox1.Count-1;
if FP > Self.ListBox1.Count-1 then FP := 0;
Self.ListBox1.ItemIndex := FP;
F := Self.ListBox1.Items[FP];
Self.Caption := Format('No.%d:正在播放%s',[FP, F]);
Self.MediaPlayer1.FileName := F;
Self.MediaPlayer1.Open;
Self.MediaPlayer1.Play;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Self.Button1.Caption := '添加列表';
Self.Button2.Caption := '播放';
Self.Button3.Caption := '上一曲';
Self.Button4.Caption := '下一曲';
Self.Button5.Caption := '清空列表';
Self.CheckBox1.Caption := '循环播放';
Self.CheckBox1.Checked := True;
{以下是为了测试方便添加的试听音乐}
Self.ListBox1.Items.Add('C:\WINDOWS\Media\Windows XP 启动.wav');
Self.ListBox1.Items.Add('C:\WINDOWS\Media\Windows XP 关机.wav');
Self.ListBox1.Items.Add('C:\WINDOWS\Media\Windows XP 登录音.wav');
Self.ListBox1.Items.Add('C:\WINDOWS\Media\Windows XP 注销音.wav');
Self.ListBox1.Items.Add('C:\WINDOWS\Media\tada.wav');
end;procedure TForm1.MediaPlayer1Notify(Sender: TObject);
begin
if Self.CheckBox1.Checked then
if Self.MediaPlayer1.NotifyValue = nvSuccessful then
Self.PlayLists(1);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
if Self.OpenDialog1.Execute then
Self.ListBox1.Items.Add(Self.OpenDialog1.FileName);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
with TButton(Sender) do begin
case Tag of
0: begin
FP := Self.ListBox1.ItemIndex;
Self.PlayLists(0);
Caption := '暂停';
end;
1: begin
Self.MediaPlayer1.Pause;
Caption := '播放';
end;
end;
Tag := Byte(not Boolean(Tag));
end;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
Self.PlayLists(-1);
end;procedure TForm1.Button4Click(Sender: TObject);
begin
Self.PlayLists(1);
end;procedure TForm1.Button5Click(Sender: TObject);
begin
Self.ListBox1.Clear;
end;end.