文章来源:uddu 97年9月(文章太长,
我没贴全,if you can't find it,you can email me)
UNDU, Sept 1997 article "Playing and Recording Sound in Delphi" by Darryl Gove
你还可以使用控件Audio 4.0(可以从很多地方
download)
1.do
you have a soundcard?
if WaveOutGetNumDevs=0 then
application.messagebox('Error', 'No sound playing card', mb_OK);
if waveInGetNumDevs=0 then
application.messagebox('Error','No recording sound card',mb_ok);
2.Getting a handle on it.
HwaveOut:=new(PHwaveOut);
i:=waveOutOpen(HWaveOut,0,Pwaveformat(WaveFormat),form1.handle,0,CALLBACK_WINDOW);
if i<>0 then
application.messagebox('Error', 'Problem creating play handle', mb_OK);
HwaveIn:=new(PHwaveIn);
i:=waveInOpen(HWaveIn,0,Pwaveformat(WaveFormat),form1.handle,0,CALLBACK_WINDOW);
if i<>0 then
application.messagebox('Error', 'Problem creating record handle', mb_OK);
3.Being prepared
The final thing todo
is to start either playing sound or recording sound.
Todo
this we need to send packets of memory to the sound card either to play or to record on.
When you send data out to be played,
the playing starts immediately you add a packet of data,
extra packets of data are added to a queue, and played in sequence.
If youre recording then
the blocks of memory are once again added to a queue -
but they are not recorded on until you tell the computer to start recording.
If the computer runs out of packets to record on then
the recording stops.
So the first thing todo
is to get a block of memory and to set up the data block
that will tell the multimedia subsystem about it.
Tmemblock=array[0..memblocklength] of byte;
memBlock:=new(PmemBlock);
Nows a good time to put your audio data into the memory block - if youre playing audio.
Header:=new(PwaveHdr);
with header^do
begin
lpdata:=pointer(memBlock);
dwbufferlength:=memblocklength;
dwbytesrecorded:=0;
dwUser:=0;
dwflags:=0;
dwloops:=0;
end;
Except for setting the pointer to a block of memory, and the length of the block of memory, all the other fields should be set to zero - unless you want to play the same block of data multiple times.
The next step is to prepare the data, why this is necessary, Ido
nt know!
i:=waveOutPrepareHeader(HWaveOut^,Header,sizeof(TWavehdr));
if i<>0 then
application.messagebox('Out Prepare error','error',mb_ok);
i:=waveInPrepareHeader(HWaveIn^,Header,sizeof(TWavehdr));
if i<>0 then
application.messagebox('In Prepare error','error',mb_ok);
then
we need to send the new block of data to the audio device - either to be played or to be recorded on.
i:=waveOutWrite(HWaveOut^,Header,sizeof(TWaveHdr));
if i<>0 then
application.messagebox('Wave out error','error',mb_ok);
i:=waveInAddBuffer(HWaveIn^,Header,sizeof(TWaveHdr));
if i<>0 then
application.messagebox('Add buffer error','error',mb_ok);
The final thing todo
when recording sound is to start!
i:=waveInStart(HwaveIn^);
if i<>0 then
application.messagebox('Start error','error',mb_ok);
There are also commands to stop and pause recording.
4.Messages
If youre using messages to control the recording an playback of audio,
then
you need to have some handlers for the messages.
The handlers should be something like
procedure MMOutOpen(var msg: Tmessage);
message MM_WOM_OPEN;
procedure MMOutClose(var msg: Tmessage);
message MM_WOM_CLOSE;
procedure MMOutDone(var msg: Tmessage);
message MM_WOM_DONE;
procedure MMInOpen(var msg: Tmessage);
message MM_WIM_OPEN;
procedure MMInClose(var msg: Tmessage);
message MM_WIM_CLOSE;
procedure MMInDone(var msg: Tmessage);
message MM_WIM_DATA;