Computing: Free Pascal Programming

Playing a WAV file using the Windows API.

The simplest way to play an audio file from within a Lazarus/Free Pascal program, is to launch the default audio player with the file as input. This can be done using the OpenDocument procedure. Example:
    uses
        LCLIntf;
    ...
    OpenDocument('C:\Users\allu\Music\Progressive Rock\Grobschnitt - Solar Music; Live.mp3');

This works with a desktop application, but also with a Lazarus/Free Pascal command line program. In this latter case, you have to add LCLBase as project requirement (in Lazarus, use Project > Project Inspector from the menu, and in Project Inspector, choose Add > Requirement).

To play audio files "in the application", you can use one of several audio packages available. I have no experience with these, so I cannot say this one works, and this one not, or this one is better, whereas that one is easier to use. You can find details about audio libraries in the Free Pascal Wiki.

If you are running MS Windows and your audio file is WAV format, you can easily play the file using the sndPlaySound function of the Windows API. The function is described in detail on learn.microsoft.com.

The definition of the function is as follows
    sndPlaySound(lpszSound, fuSound);
where lpszSound is a null terminated string (PChar), and fuSound are predefined flags to play the sound. Possible values for the flags are described in the Microsoft article mentioned above. The function returns "True" if the function-call succeeded, otherwise it returns "False".

To call the function from Free Pascal (application or command line program), you have to use the MMSystem unit.

Typical examples of calling the function are (supposing "f" to be a string containing the path to the WAV file):

To stop playback, call the function with nil as first parameter:
    sndPlaySound(nil, SND_ASYNC);

In fact, the page at learn.microsoft.com describes the function to work a little bit differently. The first parameter would be first considered to be a system sound, defined either in the Windows registry, or the WIN.INI file. It would only be if Windows cannot find a system sound with the name specified, that the first parameter would be considered to be the name of a WAV file (if the file is not found, the function returns "False"; unless SND_NODEFAULT is specified, a default sound is played).

I'm not sure if this is really how it works on Windows 10. In fact, the following program sample plays the default sound and not the mouse click sound as indicated in the Microsoft article.
    program wavtest;
    {$mode objfpc}{$H+}
    uses
        MMSystem;
    var
        f: string;
    begin
        f := 'MouseClick';
        sndPlaySound(pChar(f), SND_ASYNC or SND_LOOP);
        write('Hit ENTER to stop the sound '); Readln;
        sndPlaySound(nil, SND_ASYNC);
        write('Hit ENTER to terminate the program '); Readln;
    end.

As an example of a GUI application that uses WAV playback by calling the sndPlaySound function, have a look at my Alphabet application.


If you find this text helpful, please, support me and this website by signing my guestbook.