Search code examples
delphimp3firemonkeydelphi-10.4-sydney

How to record microphone to MP3 with Delphi 10.4.2 on FMX Windows?


In FMX Delphi 10.4.2 I'm recording audio with this code:

  var
        fMicrophone: TAudioCaptureDevice;
begin

  fMicrophone := TCaptureDeviceManager.Current.DefaultAudioCaptureDevice;

  if fMicrophone = nil
    then
      begin
        ShowMessage('Audio capturing device not available');
        Exit;
      end;

  MicFilterStringLabel.Text := fMicrophone.FilterString;  // filter is *.wav

  RecordButton.Enabled := False;
  StopButton.Enabled := True;

      // Sets the name of the file to save the recorded data
  fMicrophone.FileName := TPath.Combine(gPreferences.TemporaryFolder, 'samplefile.mp3');

  fMicrophone.StartCapture;
end;

    // later the stop button calls fMicrophone.StopCapture;

This does not seem to create an authentic MP3 file. Perhaps it's still making a WAV file but just with the MP3 extension.

Can this component make an MP3 file? Failing that, is there a component that will make MP3 files? (The mitov components don't seem to install on Delphi 10.4.2 and I don't have an email answer back from him about that yet.)


Solution

  • According to the documentation (see Audio-Video in FireMonkey and Audio Recording), the supported capture formats are:

    • WAV on Windows
    • CAF on iOS and Mac
    • 3GP on Android

    On Windows, TAudioCaptureDevice uses DirectShow, which does not support capturing MP3, only playing it.

    So, if you want to record an MP3 file, you will have to convert the WAV file after recording it. There are plenty of 3rd party libraries you can use for that. Otherwise, you will have to capture and save the microphone audio yourself, such as by using platform APIs directly instead of using FireMonkey.