How to make a recorder with Delphi

xiaoxiao2021-03-14  211

Author: Chen Jingtao Source: Updated: March 16, 2005 Delphi is Inprise (former Borland) the company's outstanding visual programming tool that comes with the development of multimedia controls are Mediaplayer weapon. Use it for a few minutes to make a program that can play multimedia files like Skims. But it may be a few people to know that it can also be a recorder. Run Delphi, drag a MediaPlayer control on the System page to the form, the default name is MediaPlayer1. Since our program uses its own buttons, set the Visible property of MediaPlayer1 to false, and other properties remain default. Put two buttons button1 and button2. The property name of Button1 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 property is changed to false. Then switch the window to the code window and start writing code. In the program, we define a WAV file's file header, and create a WAV file with the filehead when recording, and then write the voice recorded in MediaPlayer1. Several parameters of the CreateWav process are as follows: The first Channels represents the channel, takes a single episode, and takes 2 o'clock stereo. Resolution also only two values ​​can be selected, take 8-time table 8 sound, take 16-bit 16-bit sound, and Rate represents sound frequencies, such as 11025, 22050, 44100. The greater the value, the clearer the sound, the greater the recorded files. The last parameter represents the corresponding file name. Therefore, CreateWav can have the following form: CreateWav (1, 8, 11025, 'c: /abc.wav'); // Create an 8-bit mono frequency 11025 in C-driven root directory is abc.wav WAV file Createwav (2,16,44100, 'c: /abc.wav'); // Create a 16-bit stereo frequency of 44100 in the C-drive root directory, a WAV file, a WAV file, a WAV file, foreign country, a very famous The remote control software Netbus written in Delphi has a sound listening function, which is written in this article. It first recorded the other party's voice, then transferred back, reaching the purpose of listening to each other. Of course, the premise is that the other party must install a microphone, otherwise the sound of the other party is played (such as opening the unskilled or readplay play, running the program, you can record the sound of the playback). In fact, the current network sound communication technology has developed to a certain stage, voice intercom and IP phones have also begun to mature. However, they use the VOX format or ACM format, the specific code can be downloaded in my home page http://lovejingtao.126.com. But if you are not familiar with the VOX or ACM format, you can use this paper to make your own "recorder". As for how to call the system's own avifil32.dll to make network video play, let you exchange it when you have the opportunity. This program passes the PWIN98 DELPHI5.

Unit unit1;

Interface

Useswindows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls, MPlayer;

typeTWavHeader = record // file header format definition of a Wav 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); // write a custom header Wav process procedure BtStartClick (Sender: TObject); procedure BtStopClick (Sender: TObject); private {Private declarations} public {public declarations} end; varForm1: TForm1;

IMPLEMENTATION

{$ R * .dfm}

Procedure TForm1.Createwav (1 (mono) or 2 (stereo)} resolution: Word; {8 or 16, represents 8-bit or 16-bit sound} Rate: longint; {Sound frequency, such as 11025, 22050 , 44100} fn: string {corresponding file name}; varwf: file of twavheader; wh: twavheader; beginwh.rid: = $ 46464952; wh.rlen: = 36; Wh.Wid: = $ 45564157; wh.fid: = $ 20746d66; wh.flen: = 16; wh.wformattag: = 1; wh .nchannels: = channel; wh .nsamplespersec: = rate; wh .navgbytespec: = channels * rat * (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 head} Write (WF, WH); {Write file head} closefile; {Close file} end; procedure TFORM1 .BTStartClick (Sender: TOBJECT); Begintry // Create a WAV file Temp.WavCreateWav (1, 8, 11025, (applfilepath (application.exename) 'templayer1.deviceType: = dtAutoSelect; MediaPlayer1.FileName: = (ExtractFilePath (Application.ExeName) 'Temp.wav'); MediaPlayer1.Open; MediaPlayer1.StartRecording; BtStart.Enabled: = false; BtStop.Enabled: = true; exceptBtStart.Enabled: = true; BtStop.Enabled: = false; Application.MessageBox ( 'media device initialization failed!', 'error', MB_IConERROR MB_OK); end; end; procedure TForm1.BtStopClick (Sender: TObject); begintryMediaPlayer1.Stop; MediaPlayer1. save; MediaPlayer1.Close; Application.MessageBox ( 'voice recording is finished!', 'information', MB_ICONINFORMATION MB_OK); BtStart.Enabled: = True; BtStop.Enabled: = false; exceptApplication.MessageBox ( 'save the sound file error! ',' Error ', MB_ICONERROR MB_OK); btstart.enabled: = true; btstop.enabled: = false; end; end; end.

Supplement: 1. The recording of the files that can be mass-enabled when playing the file. 2. If the system is installed, the WAV file size that may be recorded is zero, but it will generate a TMP ended file, and the extension is changed to WAV is a recorded voice file. But this situation rarely occurs. (Opportunity is almost zero ^ - ^) 3, this program is passed on the running and replayer.

转载请注明原文地址:https://www.9cbs.com/read-129435.html

New Post(0)