Voice Recording/Saving in Delphi

后端 未结 1 1630
攒了一身酷
攒了一身酷 2021-02-06 18:28

Is there a component or code that allows the following: Record a spoken word (or words) and save it/them to a file that can be played back. The file must be able to be p

相关标签:
1条回答
  • 2021-02-06 19:22

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

    If you want high control, you really should use the low-level functions. Except for PlaySound, I have never used the high-level MCI interface.

    MCI

    This is 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;
    
    0 讨论(0)
提交回复
热议问题