Tutorial: How to Record Audio Files with FireMonkey FM2

By: Tim DelChiaro

Abstract: This tutorial demonstrates how to use FireMonkey to capture audio media data

This tutorial is part of the RAD Studio XE3 online documentation. Links in the article go to the Embarcadero Doc Wiki. 

To follow along with these steps, you can buy or download a trial of an Embarcadero developer tool with FireMonkey.


    Form Design

  1. Select File > New > FireMonkey Desktop Application - Delphi > HD FireMonkey Application.
  2. Add a TGridLayout to the form. With the the layout in focus, make the following settings in the Object Inspector:
    • Align to alMostToptop.
    • ItemWidth to half of the total width of the form.
  3. To the TGridLayout add two TFlowLayout objects (the second flow layout will be used in the next tutorial: Playing Audio Files).
  4. To the first TFlowLayout add:
    • TLabel. Set its text to Capturing.
    • TButton to start recording data.
    • TButton to stop recording data.
  5. Change the name of the two buttons to RecordButton and StopButton.
  6. Set the StopButton as being disabled by setting the Enable property of the button to False.
  7. Add a TSaveDialog to the form.
  8. Add a TImage. Set the Bitmap property of the TImage to an image that is suggestive for the recording process. The usual icon used for a recording process is a red circle.
    The form should look like this:
    File:Audio Record Form Design.png

    Implementation

1. Include the FMX.Media unit in the uses section:
// Delphi version of the implementation
uses
 FMX.Media;
2.Declare a TAudioCaptureDevice public member, named Mic to the TForm1 class:
type
  TForm1 = class(TForm)
   // ...............
  public
    Mic: TAudioCaptureDevice;
3. Set the Opacity property of the image to 0. The image is visible only when the recording process starts.
4. Double-click the Record button to attach OnClick event handlers to it:
procedure TForm1.RecordButtonClick(Sender: TObject);
begin
  //get he the default microphone
  Mic := TCaptureDeviceManager.Current.DefaultAudioCaptureDevice;
  if Mic <> nil then
  begin
    //set the SaveDialog filter to choose only the supported extension  
    SaveDialog1.Filter :=  Mic.FilterString;
    if SaveDialog1.Execute then
    begin
      RecordButton.Enabled := false;
      StopButton.Enabled := true;
      //gets the name of the file where to save the recorded data
      Mic.FileName := SaveDialog1.FileName;
      Mic.StartCapture;
      Image1.Opacity:=1;
    end;
  end
  else
  begin
    ShowMessage('Audio capturing divice not available');
  end;
end;
5. Double-click the Stop button to attach OnClick event handlers to the Stop button:
procedure TForm1.StopButtonClick(Sender: TObject);
begin
  if (Mic <> nil) and (Mic.State = TCaptureDeviceState.Capturing) then
  begin
    Mic.StopCapture;
    Image1.Opacity := 0;
    StopButton.Enabled := false;
    RecordButton.Enabled := true;
  end;
end;

    Run the Application

1. Run the project by pressing F9.
2. Press the Record button to start recording audio data. The SaveDialog opens.
3. Choose a path and a file name to save the recorded data.
4. Press the Stop button to stop recording. If the recording is not ended by calling StopCapture method, the saved file is not properly decoded when it is played by a media player.


Server Response from: ETNASC04