Record / save voice in Delphi

Is there a component or code that allows you to: Record the spoken word (or words) and save it / them in a file that can be played. The file must be played on XP, Vista, and Windows 7. The file can be either stand-alone or stored in a data source.

[Using Delphi 7 to create XP applications and use the Absolute Database.]

+1
source share
1 answer

The functions in MMSystem.pas allow you to do this using the Windows API. You can use high-level functions like MCI and PlaySound functions or low-level functions like waveInOpen , waveInPrepareHeader , waveInProc etc.

If you need high control, you really should use low-level functions. With the exception of PlaySound, I have never used the high-level MCI interface.

MCI

This is the working code:

procedure TForm1.FormCreate(Sender: TObject);
var
  op: TMCI_Open_Parms;
  rp: TMCI_Record_Parms;
  sp: TMCI_SaveParms;
begin

  // Open
  op.lpstrDeviceType := 'waveaudio';
  op.lpstrElementName := '';
  if mciSendCommand(0, MCI_OPEN, MCI_OPEN_ELEMENT or MCI_OPEN_TYPE, cardinal(@op)) <> 0 then
    raise Exception.Create('MCI error');

  try

    // Record
    rp.dwFrom := 0;
    rp.dwTo := 5000; // 5000 ms = 5 sec
    rp.dwCallback := 0;
    if mciSendCommand(op.wDeviceID, MCI_RECORD, MCI_TO or MCI_WAIT, cardinal(@rp)) <> 0 then
      raise Exception.Create('MCI error. No microphone connected to the computer?');

    // Save
    sp.lpfilename := PChar(ExtractFilePath(Application.ExeName) + 'test.wav');
    if mciSendCommand(op.wDeviceID, MCI_SAVE, MCI_SAVE_FILE or MCI_WAIT, cardinal(@sp)) <> 0 then
      raise Exception.Create('MCI error');

  finally
    mciSendCommand(op.wDeviceID, MCI_CLOSE, 0, 0);
  end;

end;
+4
source

Source: https://habr.com/ru/post/1793231/


All Articles