(*******************************************************************************
**
** This example was derived to overcome the deficiencies of using just the
** 'sndPlaySound' function. The 'sndPlaySound' is ideal and easy to use to
** play WAV files synchronously. It allows asynchronous playing of WAV files
** as well, but there is no method of notifying the host when the sound has
** completed playing. This example was put together after studying the
** MMSYSTEM.PAS unit, and articles on the MSCDN.
**
** Andy Strong - Compuserve ID : 100716,3015
** Internet Address: andrews@nbaqsl.co.uk
**
** Released into the publicdo
main 09/05/96
**
** Controls:
** This code may be used, modified, included in applications without any
** license agreements as long as the disclaimers are accepted - ie FREEWARE
**
** Disclaimer:
** This software is released into the publicdo
main on the strict understanding
** that neither myself nor any associates or companies I work for have any
** liability explictly or implied.
**
*******************************************************************************)
unit Mciplay1;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, MMSystem;
const
WAVFILENAME = 'C:/WINDOWS/CHIMES.WAV';
{ WAV file to play }
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
wDeviceID: word;
public
{ Public declarations }
procedure MMMCNotify(var Msg: TMessage);
message MM_MCINOTIFY;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
mciOpenParms: TMCI_Open_Parms;
mciPlayParms: TMCI_Play_Parms;
begin
if waveOutGetNumDevs < 1 then
{ Any devices? }
ShowMessage('No wave devices available!')
else
begin
FillChar(mciOpenParms,
SizeOf(TMCI_Open_Parms), 0);
FillChar(mciPlayParms,
SizeOf(TMCI_Play_Parms), 0);
with mciOpenParmsdo
{ Set MCI to play WAV files }
begin
lpStrDeviceType := 'waveaudio';
lpstrElementName := WAVFILENAME;
end;
if mciSendCommand(0, MCI_OPEN,
MCI_OPEN_TYPE or MCI_OPEN_ELEMENT,
Longint(@mciOpenParms)) = 0 then
{ Open Device }
begin
wDeviceID := mciOpenParms.wDeviceID;
{ Grab Device ID for later }
mciPlayParms.dwCallback := Handle;
{ Set our Handle for Callback message }
mciSendCommand(wDeviceID, MCI_PLAY, MCI_NOTIFY,
LongInt(@mciPlayParms));
{ Kick off play of File;
await message }
end;
end;
end;
procedure TForm1.MMMCNotify(var Msg: TMessage);
begin
inherited;
{ Normal processing for message }
ShowMessage('Sound play complete!');
mciSendCommand(wDeviceID,
MCI_CLOSE, 0, 0);
{ Close device now its finished }
end;
end.