How to create a recording program using Delphi
Chen Jingtao
Delphi is an excellent visual programming tool from InPRise (formerly Borland). Its built-in Mediaplayer control is a powerful tool for developing multimedia. With it, you can create a program that can play multimedia files like Jieba in just a few minutes. But few people may know that you can also use it to make a recording program.
Run Delphi and drag a Mediaplayer control on the System page to the form. The default name is Mediaplayer1. Since our program uses its own button, set the Visible property of Mediaplayer1 to False and keep other properties at their default values. Place two more buttons, Button1 and Button2. Button1's attribute Name is changed to BtStart, Caption is changed to "Start Recording", Button2's attribute Name is changed to BtStop, Caption is changed to "Stop Recording", and the Enabled attribute is changed to False. Then switch the window to the code window and start writing code.
In the program, we define the file header format of a Wav file. When recording, we first create a Wav file with only the file header, and then write the sound recorded by Mediaplayer1 into the file. The meanings of several parameters in the CreateWav process are as follows: the first channels represent the sound channel, when 1 is taken, it represents mono, and when 2 is taken, it represents stereo. Resolution also has only two values to choose from. When 8 is selected, it represents 8-bit sound. When 16 is selected, it represents 16-bit sound. Rate represents the sound frequency, such as 11025, 22050, 44100. The larger the value, the clearer the sound, and of course, the larger the recorded file. The last parameter represents the corresponding file name. So CreateWav can have the following form:
CreateWav(1,8,11025,'C:/abc.wav');//Create an 8-bit mono Wav file named abc.wav with a frequency of 11025 in the root directory of drive C
CreateWav(2,16,44100,'C:/abc.wav');//Create a Wav file named abc.wav with a 16-bit stereo channel frequency of 44100 in the root directory of drive C
Netbus, a well-known foreign remote control software written in Delphi, has a sound monitoring function, which is written using the method of this article. It first records the other party's voice and then transmits it back to achieve the purpose of monitoring the other party. Of course, the premise is that the other party must have a microphone installed, otherwise the sound played by the other party will be monitored (for example, open Jieba or Readplay and run this program to record the played sound).
In fact, the current network sound transmission technology has developed to a certain stage, and voice intercoms and IP telephones have also begun to mature. However, they use VOX format or ACM format. The specific codes can be downloaded from my homepage http://Lovejingtao.126.com. But if you are not familiar with VOX or ACM formats, you can also use the method in this article to make your own "recorder". As for how to call the avifil32.dll that comes with the system for online video playback, I will communicate with you when I have the opportunity.
This program passed under Pwin98+Delphi5.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, MPlayer;
type
TWavHeader = record //Define a Wav file header format
rId: longint;
rLen : longint;
wId: longint;
fId: longint;
fLen : longint;
wFormatTag : Word;
nChannels : word;
nSamplesPerSec : longint;
nAvgBytesPerSec : longint;
nBlockAlign: word;
wBitsPerSample: word;
dId: longint;
wSampleLength : longint;
end;
TForm1 = class(TForm)
MediaPlayer1: TMediaPlayer;
BtStart: TButton;
BtStop: TButton;
procedure CreateWav(channels : word; resolution : word; rate : longint; fn : string);//Customize the process of writing a Wav file header
procedure BtStartClick(Sender: TObject);
procedure BtStopClick(Sender: TObject);
private
{Private declarations}
public
{Public declarations}
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.CreateWav( channels : word; { 1 (mono) or 2 (stereo) }
resolution : word; { 8 or 16, representing 8-bit or 16-bit sound}
rate: longint; {sound frequency, such as 11025, 22050, 44100}
fn: string {corresponding file name});
var
wf: file of TWavHeader;
wh : TWavHeader;
begin
wh.rId := $46464952;
wh.rLen := 36;
wh.wId := $45564157;
wh.fId := $20746d66;
wh.fLen := 16;
wh.wFormatTag := 1;
wh.nChannels := channels;
wh.nSamplesPerSec := rate;
wh.nAvgBytesPerSec := channels*rate*(resolution div 8);
wh.nBlockAlign := channels*(resolution div 8);
wh.wBitsPerSample := resolution;
wh.dId := $61746164;
wh.wSampleLength := 0;
assignfile(wf,fn); {open the corresponding file}
rewrite(wf); {Move the pointer to the beginning of the file}
write(wf,wh); {write into file header}
closefile(wf); {Close file}
end;
procedure TForm1.BtStartClick(Sender: TObject);
begin
try
//Create a Wav file Temp.wav in the current directory of the program
CreateWav(1, 8, 11025, (ExtractFilePath(application.ExeName)+ 'Temp.wav'));
MediaPlayer1.DeviceType := dtAutoSelect;
MediaPlayer1.FileName := (ExtractFilePath(Application.ExeName)+ 'Temp.wav');
MediaPlayer1.Open;
MediaPlayer1.StartRecording;
BtStart.Enabled:=false;
BtStop.Enabled:=true;
except
BtStart.Enabled:=True;
BtStop.Enabled:=false;
Application.MessageBox('Media device initialization failed!','Error',MB_ICONERROR+MB_OK);
end;
end;
procedure TForm1.BtStopClick(Sender: TObject);
begin
try
MediaPlayer1.Stop;
MediaPlayer1.Save;
MediaPlayer1.Close;
Application.MessageBox('Voice recording completed!','Message',MB_ICONINFORMATION+MB_OK);
BtStart.Enabled:=True;
BtStop.Enabled:=false;
except
Application.MessageBox('Error saving sound file!','Error',MB_ICONERROR+MB_OK);
BtStart.Enabled:=True;
BtStop.Enabled:=false;
end;
end;
end.
Supplement: 1. When playing the recorded file, you may need to increase the Wav value of the audio attribute.
2. If some other audio drivers are installed on the system, the size of the recorded Wav file may be zero, but a file ending in TMP will be generated at the same time. Changing its extension to Wav is the recorded sound file. But this rarely happens. (The chance is almost zero^-^)
3. This program can pass the sound recording while running Jieba and Replayer.