1. Home
  2. Computing & Technology
  3. Delphi Programming

Enumerating and Playing System Sounds from Delphi code

By Zarko Gajic, About.com

Delphi Plays System Sounds

To play a system sound ("application even" or "system even" as called by MS), you can call the PlaySound API function, available in the mmsystem unit.

The PlaySound function plays a sound specified by the given filename, resource, or system event. System event are, in general, associated with a sound in the registry.

Registered system sounds are located under the "AppEvents\Apps" key under HKEY_CURRENT_USER in the Registry database.

Here's how to play the "System Question" system sound:

uses MMSystem, ...

PlaySound('SystemQuestion', 0, SND_ALIAS or SND_ASYNC) ;
Note: Using the SND_ASYNC flag the sound is played asynchronously and PlaySound returns immediately after beginning the sound playback. SND_ALIAS tells the PlaySound function that the first parameter is a system-event alias. Note (also): the actual sound refrences are stored in HKEY_CURRENT_USER\AppEvents\Schemes\Apps\.Default.

Listing all System/Event Sounds

Now that you know how to play a system sound, here's how to get the list of all the registered sounds in Windows:

Drop a TButton (name: "Button1") and a TListBox (name: "ListBox1") on a Delphi form (name: "SystemSoundPlayerForm"). Handle the Form's OnCreate and Button's OnClick events as:

uses MMSystem, Registry, ...

//Button1 ... OnClikc
procedure TSystemSoundPlayerForm.Button1Click(Sender: TObject) ;
var
   soundAlias : string;
begin
   if ListBox1.ItemIndex = -1 then
   begin
     ShowMessage('Select a sound alias from the list...') ;
     Exit;
   end;

   soundAlias := ListBox1.Items[ListBox1.ItemIndex];

   PlaySound(PAnsiChar(soundAlias), 0, SND_ALIAS or SND_ASYNC) ;
end;

//SystemSoundPlayerForm ... OnCreate
procedure TSystemSoundPlayerForm.FormCreate(Sender: TObject) ;
var
   reg : TRegistry;
begin
   reg := TRegistry.Create;
   try
     reg.RootKey := HKEY_CURRENT_USER;
reg.OpenKeyReadOnly('\AppEvents\EventLabels') ;
     reg.GetKeyNames(ListBox1.Items) ;
   finally
     reg.Free;
   end;
end;

Select a sound alias from the list and hit the play button ...

Delphi tips navigator:
» How to Declare and Initialize Constant Arrays in Delphi
« Reset the TWebBrowser Delphi control to an Empty (Blank) Page

More Delphi Programming Quick Tips
Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2006 Tips
  7. Enumerating and Playing System Sounds from Delphi code

©2009 About.com, a part of The New York Times Company.

All rights reserved.