Use Delphi to develop recording programs

Source: Internet
Author: User

Delphi is an excellent visual programming tool of Inprise (formerly Borland). Its built-in Mediaplayer control is a powerful tool for developing multimedia. You can use it to create a program that can play multimedia files like the interpreter in just a few minutes. But few people may know that it can also be used as a recording program.

Run Delphi and drag a Mediaplayer control to the form on the System page. The default name is Mediaplayer1. because our program uses its own button, set the Visible attribute of Mediaplayer1 to False, keep default values for other attributes. Change the attribute Name of Button1 and Button2.Button1 to BtStart, Caption to "Start recording", Button2's attribute Name to BtStop, Caption to "stop recording", and Enabled to False. switch to the code window and start writing the code.

In the program, we define the File Header Format of a Wav file. During the recording, we first create a Wav file with only the file header, and then write the sound recorded by Mediaplayer1 into the file. Several parameters in the CreateWav process have the following meanings: The first channels represents a sound channel, the first time form sound, and the second time form sound. Resolution also has only two values to choose from. In the 8-era table, there are 8-bit sounds, and in the 16-era table, 16-bit sounds. The rate indicates the sound frequency, for example, 44100. the larger the value, the clearer the sound. Of course, the larger the recorded file. The last parameter indicates the corresponding file name. Therefore, CreateWav can be in the following format:

CreateWav (11025, 'c: abc.wav '); // create an 8-bit Wav file named abc.wav under the C-root directory.

CreateWav (44100, 'c: abc.wav '); // create a 16-bit Wav file named abc.wav under the C-root directory.

Netbus, a well-known remote control software written in Delphi, has a sound listening function, which is written in this article. It records the voice of the other party and sends it back to listen to the other party. Of course, the premise is that the other party must have a microphone installed; otherwise, it will listen to the sound played by the other party (for example, enable decoding or Readplay, and run this program, you can record the playing sound ).

In fact, the current network sound communication technology has developed to a certain stage, and voice intercom and IP Phone numbers have also begun to mature. But they are in VOX format or ACM format, the specific code can be downloaded in my home page http://Lovejingtao.126.com. However, if you are not familiar with the VOX or ACM format, you can also use this method to create your own "recorder ". As for how to call the avifil32.dll provided by the system to play online videos, I will discuss it with you when I have a chance.

This program is passed under Pwin98 + Delphi5.

Unit Unit1;
Interface
Uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, MPlayer;
Type
TWavHeader = record // defines the format of a Wav file header.
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); // you can 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 (single sound) or 2 (STEREO )}
Resolution: word; {8 or 16, representing 8 or 16 voices}
Rate: longint; {sound frequency, such as 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 pointer to file header}
Write (wf, wh); {write into the file header}
Closefile (wf); {close the file}
End;
Procedure TForm1.BtStartClick (Sender: TObject );
Begin
Try
// Create a wave file temp.wav under the program's current directory
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 ('sound recording is complete! ', 'Info', MB_ICONINFORMATION + MB_ OK );
BtStart. Enabled: = True;
BtStop. Enabled: = false;
Except
Application. MessageBox ('An error occurred while saving the audio 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 in the system, the recorded Wav file size may be zero, but a file ending with TMP will be generated at the same time, change its extension to Wav, which is the recorded audio file. But this rarely happens. (Chances are almost zero ^-^)

3. The recording sound passes through in the running of the decoding and Replayer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.