Delphi - Make a Thread that plays a song in a loop

Asked

Viewed 588 times

2

I managed to make a Thread that plays the song, but only once. If I put a loop on it I have several errors, like the 1400.

It seems that the thread does not close when you close Form1, because the problem always comes when I try to close it.

I have only 2 Units. 1 is Form1 and the other is Tmusica thread.

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

Call from the Tmusica Thread:

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;

A Thread Tmusica:

procedure TMusica.Execute; 
begin 
  Form1.MediaPlayer1.FileName: = 'music.mp3';
  Form1.MediaPlayer1.Open;
  Form1.MediaPlayer1.Play;
end;
  • Put there a piece of code that plays the music! To help you we need to see what the code, not to be kicking and kicking!

  • Follow my project. It’s super small, thank you for helping. http://www.mediafire.com/download/d6dqwba74ijodxx/Projects.zip

  • @Victordm put the relevant code to the question instead of putting a link. That way it becomes easier to help you. In this case I would say to put the relevant code to thread of sound

1 answer

6

Well, I analyzed your project and you don’t have to do all that to work, just follow the simple procedures to follow:

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

In the form Oncreate event add these procedures:

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

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

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

The two components I mentioned are on the Tab System.

  • Thank you, it worked perfectly!!

  • If it worked, it was useful, mark the answer as Correct to make it easier for others to find solutions!

Browser other questions tagged

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