How to create video playlist in Delphi

Asked

Viewed 501 times

1

I’m trying to create a playlist of videos in Delphi, to play the videos by Mediaplayer, however, does not execute all the chosen videos, only the first 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 hint?

1 answer

0


I’m trying to create a video playlist on Delphi, to play videos by the mediaplayer and does not run all videos chosen, only the first who rotates...

This happens because before setting the file in Mediaplayer, the index is changed 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;

Updating

  1. Create a global variable called VideoAtual:

    var
      Form1: TForm1;
      VideoAtual: Integer = 0;
    
  2. Popule the 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;
    
  3. 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;
    
  4. At the 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;
    
  • Still not running the added videos, runs only 1 and to.

  • @Renatosouza You select before the video on ListBox?

  • the code is not to select, it must take the items of listbox1 and put in the player line, even if I run Opendialog and then selecting will not.

  • @Renatosouza I updated the answer.

  • Friend, I appreciate your immense help. But as a beginner, I’m breaking my head to know where I put this code, where I populate. You can help me if you don’t want to. @qmechanik

  • @Renatosouza Dispo. See that example. I created it in Delphi XE3, if you’re using a very old version, you might not be able to compile it, but just look at the code.

  • @Renatosouza If it works, mark the answer as accepted by clicking on the below of the votes. =)

  • @qmwchanik It rotates all the videos I select when I click on search videos? It did not rotate here, only rotates 1 and ready, does not skip to the next ones.

  • The following is a download of my project. https://mega.co.nz/#! fFh3Rb5R! 4QAicX8m7lxV2Ad5V9y8vGs6gfNAmbSv5Ra5pVztyA

  • @Renatosouza I modified that example of mine, see here, just click on "Create Playlist" only. I tested with songs.

  • thanks friend, it worked out, now I will implement to run the images together and ready. Thanks

  • you know how I can make this player take the list of videos from a text file, in txt I have the paths one at the bottom of the other and then I want to play them straight, without having to open the opendialog.

  • @Renatosouza Well, I think you can put the path of images in a array and in the Timer you put the images in a component TImage. If you can’t do it, create a question that I try to help better.

  • as I do to be able to run the videos without using opendialog, I will add the videos by an external txt.

  • @Renatosouza Just read line by line from txt, first you’ll have to open it using AssignFile, from there on a loop you read line-by-line using the Readln. If I can’t do this, create a new question then I try to respond better.

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.