GARANTIR DESCONTO

Fórum Componente Media Player do delphi #595658

23/07/2018

0

Boa Noite Pessoal!
Fiz um simples sistema para tocar musica usando o componente media player do delphi, um listbox para lista de musica, um timer e progressbar para ver o andamento.
Gostaria de saber como faço para fechar o form quando a musica terminar de executar
Aqui está o Código que estou usando obrigado

unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Mask, Vcl.DBCtrls,
Vcl.DBCGrids, Data.DB, Data.Win.ADODB, Vcl.FileCtrl, Vcl.OleCtrls, WMPLib_TLB,
Vcl.ExtCtrls, Vcl.ComCtrls, Vcl.MPlayer, Vcl.Menus, MMSystem;

type
TForm1 = class(TForm)
DBCtrlGrid1: TDBCtrlGrid;
DBEdit1: TDBEdit;
DBImage1: TDBImage;
FileListBox1: TFileListBox;
ADOQuery1: TADOQuery;
ADOQuery1ID: TAutoIncField;
ADOQuery1caminho: TWideStringField;
ADOQuery1pasta: TWideStringField;
ADOQuery1imagem: TBlobField;
DataSource1: TDataSource;
DirectoryListBox1: TDirectoryListBox;
DBImage2: TDBImage;
ListBox1: TListBox;
Edit1: TEdit;
Play: TButton;
Adicionar: TButton;
Edit2: TEdit;
Timer1: TTimer;
Timer2: TTimer;
Panel1: TPanel;
OpenDialog1: TOpenDialog;
Procurar: TButton;
Remover: TButton;
Parar: TButton;
procedure DBCtrlGrid1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure DBCtrlGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure FileListBox1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure AdicionarClick(Sender: TObject);
procedure PlayClick(Sender: TObject);
procedure Timer2Timer(Sender: TObject);
procedure FormKeyPress(Sender: TObject; var Key: Char);
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure MudaFaixa(Valor : Integer);
procedure ProcurarClick(Sender: TObject);
procedure RemoverClick(Sender: TObject);
procedure PararClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
Lista : TStringList; //variável que armazenará a sequência das músicas
PLAYING : Boolean = FALSE; //guarda estado do tocador; padrão é false
Cont : Integer = 0; //conta em qual faixa está
Tempo : Integer = 0;

implementation
uses unit2;

{$R *.dfm}

procedure Tform1.MudaFaixa(Valor : Integer);
begin
{ "Valor" guarda o sinal: 1 para próxima faixa
e -1 para faixa anterior }

if (Valor <> -1) and (Valor <> 1) then exit;

if PLAYING then begin
form2.MediaPlayer1.Stop;
Timer1.Enabled := False;
PLAYING := FALSE;
end;

Cont := Cont + Valor;

//testa se não passou da última ou se não se está tentando
//voltar para antes da primeira música
if (Cont > (ListBox1.Items.Count -1)) then
Cont := 0
else
if (Cont < 0) then
Cont := (ListBox1.Items.Count -1) ;

form2.MediaPlayer1.Close;

form2.MediaPlayer1.FileName := Lista.Strings[Cont];
ListBox1.Selected[Cont]:=true;
form2.MediaPlayer1.Open;
form2.Show;
form2.MediaPlayer1.Play;
form2.MediaPlayer1.DisplayRect := Form2.ClientRect;
Timer1.Enabled := True;
PLAYING := True;
end;

procedure TForm1.PararClick(Sender: TObject);
begin
if PLAYING then begin
form2.MediaPlayer1.Stop;
PLAYING := FALSE;
Timer1.Enabled := False; //pára o timer
end;
end;

procedure TForm1.PlayClick(Sender: TObject);
begin
{ se já estava tocando, não aciona PLAY de novo e, sim,
RESUME, para prosseguir do ponto onde parou }
if PLAYING then form2.MediaPlayer1.Resume
else begin
{ vai iniciar uma faixa, verifica qual
é através da variável Cont, atribui ao MediaPlayer
e atribui também à ListBox }
form2.MediaPlayer1.Close;
form2.MediaPlayer1.FileName := Lista.Strings[Cont];
ListBox1.Selected[Cont] := True;
form2.MediaPlayer1.Open;
form2.Show;
form2.MediaPlayer1.Play;
form2.MediaPlayer1.DisplayRect := Form2.ClientRect;
form2.MediaPlayer1.Play;
Timer1.Enabled := True;//habilita timer
timer2.Enabled := true;
end;
PLAYING := True;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
{ Verifica o andamento da música do MediaPlayer;
se posição da faixa = largura, então
muda para a faixa seguinte }
var
X : integer;
begin
if form2.MediaPlayer1.Position = form2.MediaPlayer1.Length then
begin
with form2.MediaPlayer1 do begin
Cont := Cont + 1;
if Cont > Lista.Count-1 then
Cont := 0;
Close ;

if FileExists(Lista.Strings[Cont]) then begin
TimeFormat := tfHMS;
FileName := Lista.Strings[Cont];
Open ;
Play;
end
else
Cont := Cont + 1;
end; //with MediaPlayer
end; //if MediaPlayer

ListBox1.Selected[Cont];

if PLAYING then
begin
form2.ProgressBar1.Max := form2.MediaPlayer1.Length;
form2.ProgressBar1.Position := form2.MediaPlayer1.Position;
end;


begin
for x:=0 to 100 do
begin
form2.ProgressBar1.Position:=form2.ProgressBar1.Position+1;
Sleep(90);
end;
form2.ProgressBar1.Position:=0;
close;
END;
end;


procedure TForm1.Timer2Timer(Sender: TObject);
begin
if timer2.Interval = 5000 then
begin
form2.Show;
end;

end;


procedure TForm1.AdicionarClick(Sender: TObject);
var chanson: WMPlib_TLB.IWMPMedia;
begin
Lista.Add(FileListBox1.FileName);
edit1.Text := ExtractFileName(FileListBox1.FileName);
listbox1.Items.Add(edit1.Text);
listbox1.ItemIndex := 0;
edit2.Text := listbox1.Items[Listbox1.ItemIndex];


end;

procedure TForm1.ProcurarClick(Sender: TObject);
begin
//Chama a caixa de diálogo de abrir arquivos
//para ir montando a PlayList

if OpenDialog1.Execute then begin
Lista.Add(OpenDialog1.FileName);
ListBox1.Items.Add(ExtractFileName(OpenDialog1.FileName));
end;
end;

procedure TForm1.RemoverClick(Sender: TObject);
begin
if application.messagebox(pchar(''''''''''''''''''''''''''''''''Remover faixa ''''''''''''''''''''''''''''''''+ListBox1.Items.strings[ListBox1.ItemIndex]+''''''''''''''''''''''''''''''''?''''''''''''''''''''''''''''''''),pchar(Caption),36)=IDYES then begin
pararclick(self);
Lista.Delete(ListBox1.ItemIndex);
ListBox1.DeleteSelected;
if Cont > 0 then
Cont := Cont - 1;
end;
end;

procedure TForm1.DBCtrlGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
begin
if (Key = VK_DOWN) then
begin
FileListBox1.SetFocus;
filelistbox1.ItemIndex := 0;
Key := 0;
end
else
begin
if Key = VK_UP then
begin
FileListBox1.SetFocus;
filelistbox1.ItemIndex := 0;
Key := 0;
end;
end;

end;
end;

procedure TForm1.DBCtrlGrid1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
FileListBox1.ApplyFilePath(dbedit1.Text);
end;

procedure TForm1.FileListBox1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_LEFT then
begin
DBCtrlGrid1.SetFocus;
end
else
begin
if key = VK_RIGHT then
begin
DBCtrlGrid1.SetFocus;
end;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var i : Integer;
begin
//torna o alinhamento do Painel 3 "alClient", para preencher
//toda a área disponível do form

//cria a variável (que guardará a Playlist) na memória
Lista := TSTringList.Create;

//se já existe uma playlist, carrega-a
if FileExists(ExtractFilePath(ParamStr(0))+''''''''''''''''''''''''''''''''Lista.txt'''''''''''''''''''''''''''''''') then begin
Lista.LoadFromFile(ExtractFilePath(ParamStr(0))+''''''''''''''''''''''''''''''''Lista.txt'''''''''''''''''''''''''''''''');
{ Para mostrar músicas no painel, remove caminho de diretórios,
extraindo apenas os nomes dos arquivos }
for i:=0 to Lista.Count-1 do
ListBox1.Items.Add(ExtractFileName(Lista.Strings[i]));
end;

//obtém volume para ajustar TrackBar1

//carrega imagem

end;


procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
if key = #28 then
begin
timer2.Enabled := true;
end;

end;

end.
Fabio Paula

Fabio Paula

Responder

Post mais votado

24/07/2018

Basta verificar se o index da sua lista é igual a quantidade de arquivos e o status do play é = stop

Raimundo Pereira

Raimundo Pereira
Responder

Gostei + 2

Mais Posts

26/07/2018

Fabio Paula

Qual index do listbox ou media player
Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar