Delphi - Make a Thread that plays a song in loop

2

I managed to make a Thread that plays the song, but only for 1 time.  If I put a loop in it I have several errors, like the 1400.

It seems that the thread does not close when Form1 is closed, because the problem always happens when I try to close it.

I have only 2 units. 1 is Form1 and the other is the TMusica thread.

I've tried the Goto, While, Repeat, With. Nothing works, the song is called music.mp3. I use Delphi XE8 . How do I solve it?

TMusica Thread Call:

procedure TForm1.FormCreate(Sender: TObject);  
var
  Thread:TMusica;
begin
   //Bloco de Códigos
  Thread := TMusica.Create(False);  
  Thread.FreeOnTerminate := True;
  Thread.Resume;
   //Outros códigos...
 end;

The TMusica Thread:

procedure TMusica.Execute; 
begin 
  Form1.MediaPlayer1.FileName: = 'music.mp3';
  Form1.MediaPlayer1.Open;
  Form1.MediaPlayer1.Play;
end;
    
asked by anonymous 02.11.2015 / 19:48

1 answer

6

Well, I've analyzed your project and you do not have to do it all to work, just follow the simple procedures below:

Add a TMediaPlayer component to the form and change the Visible property of it to False;

In the OnCreate event of the form add these procedures:

procedure frmExemplo.FormCreate(Sender: TObject);
begin
  MediaPlayer1.Close;
  MediaPlayer1.FileName:=('d:.mp3'); //Caminho do seu arquivo Mp3
  MediaPlayer1.Open;
  MediaPlayer1.Play;
end;

Add a component TTimer , change the Interval property from it to 84000 (84000 corresponds to 1 minute and 24 seconds in Milliseconds), that is, when exactly the song finishes it will play again:

procedure frmExemplo.Timer1Timer(Sender: TObject);
begin
  MediaPlayer1.Play;
end;

The two components I mentioned are in the% Aaa% tab.

    
02.11.2015 / 21:06