无法使用Media Player或者RealPlayer及网络上的声音文件通过声卡发出声音的声音信号进行录音,请教高手指点指点。
我使用的程序如下:
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.