录音和回放不难啊!难得是语音识别啊兄弟!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, MPlayer;
type
TWavHeader =record //定义一个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);//自定义写一个Wav文件头过程
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(单声)或者2(立体声) }
resolution : word;
{ 8或者16,代表8位或16位声音 }
rate : longint;
{ 声音频率,如11025,22050, 44100}
fn : string { 对应的文件名称 } );
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);
{打开对应文件 }
rewrite(wf);
{移动指针到文件头}
write(wf,wh);
{写进文件头 }
closefile(wf);
{关闭文件 }
end;
procedure TForm1.BtStartClick(Sender: TObject);
begin
try//在程序当前目录下创建一个Wav文件Temp.wav
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('媒体设备初始化失败!','错误',MB_ICONERROR+MB_OK);
end;
end;
procedure TForm1.BtStopClick(Sender: TObject);
begin
try
MediaPlayer1.Stop;
MediaPlayer1.Save;
MediaPlayer1.Close;
Application.MessageBox('声音录制完毕!','信息',MB_ICONINFORMATION+MB_OK);
BtStart.Enabled:=True;
btStop.Enabled:=false;
except
Application.MessageBox('保存声音文件出错!','错误',MB_ICONERROR+MB_OK);
BtStart.Enabled:=True;
BtStop.Enabled:=false;
end;
end;
end.
补充:1、录制的文件播放时可能要把音频属性的Wav调大。
2、如果系统安装了其它一些音频驱动程序,则可能录制的Wav文件大小为零,但会同时生成一个TMP结尾的文件,将其扩展名改为 Wav就是录制的声音文件。但这种情况很少发生。(机会几乎为零^-^)
3、本程序在运行解霸和Replayer下录制声音通过。