unit UnitRecord;interfaceuses
  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;    MediaPlayer: TMediaPlayer;    procedure CreateWav(channels : Word; resolution : Word; rate : LongInt; fn : string);//自定义写一个Wav文件头过程
    procedure RecordStar(var MediaPlayer: TMediaPlayer);
    procedure RecordStop(var MediaPlayer: TMediaPlayer);end;implementationprocedure 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 RecordStar;
var
  TempWavName: string;
begin
  TempWavName := FormatDateTime('yyyymmddhhnnss', now);
  try
    CreateWav(1, 8, 11025, (ExtractFilePath(Application.ExeName)+ TempWavName));
    MediaPlayer.DeviceType := dtAutoSelect;
    MediaPlayer.FileName := (ExtractFilePath(Application.ExeName)+ TempWavName);
    MediaPlayer.Open;
    MediaPlayer.StartRecording;
  except
    MessageBox(Handle, PChar('录音设备出错'), '提示信息', MB_OK + MB_ICONWARNING);
  end;
end;procedure RecordStop;
begin
  try
    MediaPlayer.Stop;
    MediaPlayer.Save;
    MediaPlayer.Close;
  except
    MessageBox(Handle, PChar('录音文件保存出错'), '提示信息', MB_OK + MB_ICONWARNING);
  end;
end;end.
很显然,这个代码是无法编译的,但是,对于新手的我来说,不知道如何才能让这个单元编译过去,请高手赐教!

解决方案 »

  1.   

    你的MessageBox第一个参数handle,是不确定的可以写为Application.handle,或是哪个窗体调用你这个单元就用它的handle.
    其次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;  MediaPlayer: TMediaPlayer;  procedure CreateWav(channels : Word; resolution : Word; rate : LongInt; fn : string);//自定义写一个Wav文件头过程
      procedure RecordStar(var MediaPlayer: TMediaPlayer);
      procedure RecordStop(var MediaPlayer: TMediaPlayer);end;implementation修改为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;  procedure CreateWav(channels : Word; resolution : Word; rate : LongInt; fn : string);//自定义写一个Wav文件头过程
      procedure RecordStar(var MediaPlayer: TMediaPlayer);
      procedure RecordStop(var MediaPlayer: TMediaPlayer);
    var
      MediaPlayer: TMediaPlayer;implementation