Search code examples
lazarusbackground-musicplaysound

Lazarus play music in loop


I've been working on a little tool the past few days and the functionality itself is fine. I wanted to make the tool a little nicer to work with (at least for me) and included a sound-file (called test.wav) which plays one of my favorite songs.

The idea was that the song constantly repeats itself but after one go through, the music stops. This is my code-snippet for the music-part:

procedure TForm1.music();
begin
    PlaySound('test.wav', 0, SND_ASYNC);
end;   

When i change SND_ASYNC to SND_LOOP and start debug my project my .exe-Windows does not appear but the music plays (also just once).

Also the Procedure is called at the project.lpr:

begin
      ...
      Form1.music();
      Application.Run; 

So how do I get the music to play in a constant loop? It doesn't have to be .wav btw.


Solution

  • The PlaySound function is (still) documented

    The interesting setting is in the third parameter fdwSound and there what is said about SND_LOOP:

    SND_LOOP The sound plays repeatedly until PlaySound is called again with the pszSound parameter set to NULL. If this flag is set, you must also set the SND_ASYNC flag.

    Note the last sentence. So the proper call would be:

    PlaySound('test.wav', 0, SND_ASYNC or SND_LOOP);
    

    To activate it automatically at program start, use the forms OnCreate() event.