Les fonctions MMSystem.pas vous permettent de le faire en utilisant l'API de Windows. Vous pouvez utiliser des fonctions de haut niveau tels que le MCI functions et PlaySound, ou des fonctions de bas niveau tels que waveInOpen, waveInPrepareHeader, waveInProc etc.
Si vous voulez un contrôle élevé, vous devez vraiment utiliser les fonctions de bas niveau. Sauf pour PlaySound, je n'ai jamais utilisé l'interface MCI de haut niveau.
MCI
C'est le code de travail:
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;