How to create video playlist in Delphi

1

I'm trying to create a video playlist in Delphi to play the videos through the MediaPlayer, but it does not run all the videos you've chosen, just the first one that runs, see the code:

procedure TForm1.Button4Click(Sender: TObject);
var 
  i:integer;
  Cont: Integer;
begin
  Cont := 0;
  if OpenDialog3.Execute  then
  begin
    for i := 0 to OpenDialog3.Files.count -1 do
      ListBox1.Items.Add(OpenDialog3.Files[i]);
    ListBox1.ItemIndex := 0;
    MediaPlayer1.FileName := ListBox1.Items[listbox1.ItemIndex];
    MediaPlayer1.Open;
    MediaPlayer1.Play;
  end;
end;

Any tips?

    
asked by anonymous 06.05.2015 / 05:17

1 answer

0
  

I'm trying to create a playlist of videos in delphi, to play the   videos by the mediaplayer and does not run all the chosen videos,   only the first one that runs ...

This is because before setting the file in MediaPlayer, the index changes to 0 , it is necessary to remove the line ListBox1.ItemIndex .

procedure TForm1.Button4Click(Sender: TObject);
var 
  i, Cont: integer;
begin
  Cont := 0;
  if OpenDialog3.Execute  then
  begin
    for i := 0 to OpenDialog3.Files.count -1 do
      ListBox1.Items.Add(OpenDialog3.Files[i]);
    //ListBox1.ItemIndex := 0;
    MediaPlayer1.FileName := ListBox1.Items[listbox1.ItemIndex];
    MediaPlayer1.Open;
    MediaPlayer1.Play;
  end;
end;

Update

  • Create a global variable named VideoAtual :

    var
      Form1: TForm1;
      VideoAtual: Integer = 0;
    
  • Populate Listbox with the method below:

    procedure CriarPlayList;
    var
      I: integer;
    begin
      if OpenDialog1.Execute = False then exit;
    
      for I := 0 to OpenDialog1.Files.count -1 do
        ListBox1.Items.Add(OpenDialog1.Files[I]);
      ListBox1.ItemIndex := 0;
      MediaPlayer1.FileName := ListBox1.Items[ListBox1.ItemIndex];
      MediaPlayer1.Open;
      MediaPlayer1.Play;
    end;
    
  • To run the video, use the method below:

    procedure ExecutarVideo(Indice: integer);
    begin
      if Indice >= ListBox1.Items.Count then begin
        VideoAtual := 0;
        Indice := 0;
      end;
      ListBox1.ItemIndex := Indice;
      MediaPlayer1.Close;
      MediaPlayer1.FileName := ListBox1.Items[ListBox1.ItemIndex];
      MediaPlayer1.Open;
      MediaPlayer1.Play;
    end;
    
  • In event Onclick of MediaPlayer , add code:

    case Button of
       btNext: begin
          VideoAtual := VideoAtual + 1;
          ExecutarVideo(VideoAtual);
       end;
       btPrev: begin
          VideoAtual := VideoAtual - 1;
          ExecutarVideo(VideoAtual);
       end;
    end;
    
  • 06.05.2015 / 05:20