请问如何实现播放完后一首歌曲后自动播放下一首(50分)

  • 主题发起人 主题发起人 shell~
  • 开始时间 开始时间
S

shell~

Unregistered / Unconfirmed
GUEST, unregistred user!
请问如何实现播放完后一首歌曲后自动播放下一首
我在listbox中显示歌曲文件
 
下面是我以前写的一个程序,用DELPHI的MEDIAPLAYER控件做的,方法有些土,但是刚好实现了你说的功能,希望能帮帮你
interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
OleCtrls, chartfx3, ExtCtrls,StdCtrls, MPlayer, ComCtrls, Menus;

type
TForm1 = class(TForm)
MediaPlayer1: TMediaPlayer;
Timer1: TTimer;
GroupBox1: TGroupBox;
Label2: TLabel;
Label3: TLabel;
Label1: TLabel;
Label4: TLabel;
Panel1: TPanel;
GroupBox2: TGroupBox;
ListBox1: TListBox;
BtnPlay: TButton;
BtnStop: TButton;
BtnPause: TButton;
BtnPrev: TButton;
BtnNext: TButton;
BtnAbout: TButton;
BtnOpen: TButton;
TrackBar1: TTrackBar;
DlgOpenfile: TOpenDialog;
PopupMenu1: TPopupMenu;
N1: TMenuItem;
N2: TMenuItem;
BtnExit: TButton;
N3: TMenuItem;
Label5: TLabel;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure BtnAboutClick(Sender: TObject);
procedure BtnPlayClick(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure BtnStopClick(Sender: TObject);
procedure BtnPauseClick(Sender: TObject);
procedure TrackBar1Enter(Sender: TObject);
procedure TrackBar1Change(Sender: TObject);
procedure BtnOpenClick(Sender: TObject);
procedure ListBox1DblClick(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure BtnNextClick(Sender: TObject);
procedure BtnPrevClick(Sender: TObject);
procedure N1Click(Sender: TObject);
procedure BtnExitClick(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure N301Click(Sender: TObject);
procedure N11Click(Sender: TObject);
procedure N51Click(Sender: TObject);
procedure N101Click(Sender: TObject);
procedure N12Click(Sender: TObject);
procedure Label5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;


var
Form1: TForm1;
//TmpPosition:integer;
DragFlag:boolean;
appdir:string;
implementation

uses Unit2, Unit3;

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
HisText:TextFile;
strtmpName:string;
begin

getdir(0,appdir);
if FileExists(appdir+'/CatPlayer.ini')then

begin


AssignFile(HisText,appdir+'/CatPlayer.ini');
reset(histext);
try
while not eof(histext)do
begin

readln(histext,strtmpName);
if FileExists(strtmpName) then

ListBox1.Items.Add(strtmpName);
end;

finally
CloseFile(histext);
end;

end;

Timer1.Enabled:=false;
BtnPlay.Enabled:=false;
end;


procedure TForm1.Timer1Timer(Sender: TObject);
begin

Label4.Caption:=IntToStr(Mediaplayer1.Length div 1000)+' 秒';
TrackBar1.Position:=MediaPlayer1.Position;
if MediaPlayer1.Position=Mediaplayer1.length then

BtnNext.Click;
//Mediaplayer1.Play;
if ListBox1.Items.Count>0 then

BtnPlay.Enabled:=true
else

BtnPlay.Enabled:=false;
end;


procedure TForm1.BtnAboutClick(Sender: TObject);
begin

frmAbout.showmodal;
end;


procedure TForm1.BtnPlayClick(Sender: TObject);
var
strMusicName:string;
begin

if ListBox1.ItemIndex<0 then

exit;
MediaPlayer1.FileName:=ListBox1.Items[ListBox1.ItemIndex];
MediaPlayer1.Open;
TrackBar1.Max:=Mediaplayer1.Length;
MediaPlayer1.Play;
strMusicName:=MediaPlayer1.FileName;
while pos('/',strMusicName)>0do

begin

StrMusicName:=copy(StrMusicName,pos('/',strMusicName)+1,Length(strMusicName));

end;

Form1.Caption:='CatPlayer '+copy(StrMusicName,1,pos('.',strmusicname)-1);
Timer1.Enabled:=true;
end;


procedure TForm1.ListBox1Click(Sender: TObject);
begin

BtnPlay.Enabled:=true;
end;


procedure TForm1.BtnStopClick(Sender: TObject);
begin

MediaPlayer1.Stop;
end;


procedure TForm1.BtnPauseClick(Sender: TObject);
begin

MediaPlayer1.Pause;
end;


procedure TForm1.TrackBar1Enter(Sender: TObject);
begin

DragFlag:=true;
end;


procedure TForm1.TrackBar1Change(Sender: TObject);
begin

Label1.caption:=InttoStr(MediaPlayer1.Position div 1000)+' 秒';
end;


procedure TForm1.BtnOpenClick(Sender: TObject);
var
i:integer;
begin

DlgOpenFile.Filter := 'MP3歌曲 (*.mp3)|*.mp3|视频剪辑(*.avi,*.asf)|*.avi;*.asf|vcd文件(*.dat)|*.dat|其他类型(*.*)|*.*';
DlgOpenFile.Execute;
with DlgOpenFile.Filesdo

begin

for i:=0 to count-1do

begin

ListBox1.Items.Add(Strings);
end;

end;

BtnPlay.Enabled:=true;
end;


procedure TForm1.ListBox1DblClick(Sender: TObject);
begin

BtnPlay.Click;
end;


procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
var
MusicStrLst:TstringList;
i:integer;
begin

MusicStrLst:=Tstringlist.Create;
for i:=0 to ListBox1.Items.Count-1do

begin

MusicStrLst.Add(ListBox1.Items);
end;

MusicStrLst.SaveToFile(appdir+'/CatPlayer.ini');
MusicStrLst.Destroy;
end;


procedure TForm1.BtnNextClick(Sender: TObject);
begin

if ListBox1.ItemIndex=ListBox1.Items.Count-1 then

ListBox1.ItemIndex:=0
else

ListBox1.ItemIndex:=ListBox1.ItemIndex+1;
BtnPlay.Click;
end;


procedure TForm1.BtnPrevClick(Sender: TObject);
begin

if ListBox1.itemIndex>0 then

begin

ListBox1.ItemIndex:=ListBox1.ItemIndex-1;
BtnPlay.Click;
end;

end;


procedure TForm1.N1Click(Sender: TObject);
begin

if ListBox1.Items[ListBox1.ItemIndex]<>''then

begin

ListBox1.Items.Delete(ListBox1.ItemIndex);
BtnPrev.Click;
end;

end;


procedure TForm1.BtnExitClick(Sender: TObject);
begin

Close;
end;


procedure TForm1.N2Click(Sender: TObject);
begin

BtnOpen.Click;
end;


procedure TForm1.N3Click(Sender: TObject);
begin

// MediaPlayer1.Stop;
MediaPlayer1.Close;
ListBox1.Clear;
timer1.Enabled:=false;
end;


procedure TForm1.N301Click(Sender: TObject);
begin

MediaPlayer1.Position:=MediaPlayer1.Position+30*1000;
MediaPlayer1.Play;
end;


procedure TForm1.N11Click(Sender: TObject);
begin

//MediaPlayer1.Stop;
MediaPlayer1.Position:=MediaPlayer1.Position+60*1000;
MediaPlayer1.Play;
end;


procedure TForm1.N51Click(Sender: TObject);
begin

MediaPlayer1.Position:=MediaPlayer1.Position+5*60*1000;
MediaPlayer1.Play;
end;


procedure TForm1.N101Click(Sender: TObject);
begin

MediaPlayer1.Position:=MediaPlayer1.Position+10*60*1000;
MediaPlayer1.Play;
end;


procedure TForm1.N12Click(Sender: TObject);
begin

MediaPlayer1.Position:=MediaPlayer1.Position+1000;
MediaPlayer1.Play;
end;


procedure TForm1.Label5Click(Sender: TObject);
begin

if Form1.MediaPlayer1.FileName<>''then

Form3.ShowModal;
end;


end.


实现连续播放关键在这里
procedure TForm1.Timer1Timer(Sender: TObject);
begin

Label4.Caption:=IntToStr(Mediaplayer1.Length div 1000)+' 秒';
TrackBar1.Position:=MediaPlayer1.Position;
[red]if MediaPlayer1.Position=Mediaplayer1.length then

BtnNext.Click;[/red]
//Mediaplayer1.Play;
if ListBox1.Items.Count>0 then

BtnPlay.Enabled:=true
else

BtnPlay.Enabled:=false;
end;

 
先得到你listbox中文件数目设为m,然后你可以自己定义数据i由0开始对应歌曲了,只要
歌曲结束,就j:=inc(i)。(至于歌曲结束部分如何确定,很多地方都有介绍的,自己搜索一
下)。得到的数整除就可以实现不断重复了。(j)div(m)
 
不知道上面的有没有说计数器。我就是用一个计数器啊。
算出一首歌的时间,改变计数器的长度。

呵呵,笨一点,也挺好的。

关注一下,明天早上来看看高手的言论吧。。。。
 
后退
顶部