Audio Recording

From Appmethod Topics
Jump to: navigation, search

Go Up to Tutorial: FireMonkey Audio-Video

This tutorial demonstrates how to use FireMonkey to capture audio media data.

Supported File Formats

We recommend that you save and use media files in the following formats according to the platform:

  • .wav on Windows
  • .caf on iOS and Mac OS X
  • .3gp on Android

You also can play other types of media files, such as MP3 files.

Form Design

  1. Select File > New > FireMonkey Desktop Application - Object Pascal > HD FireMonkey Application.
  2. Add a TGridLayout to the form. With the layout in focus, make the following settings in the Object Inspector:
  3. Add two TFlowLayout objects to the TGridLayout (the second flow layout will be used in the next tutorial: Playing Audio Files).
  4. Add the following to the first TFlowLayout:
    • A TLabel. Set its text to Capturing.
    • A TButton to start recording data.
    • A TButton to stop recording data.
  5. Change the name of the two buttons to RecordButton and StopButton.
  6. Set the StopButton as 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:
    Audio Record Form Design.png


1. Include the FMX.Media unit in the uses section:
// Delphi version of the implementation
2. Declare a TAudioCaptureDevice public member, named Mic, to the TForm1 class:
  TForm1 = class(TForm)
   // ...............
    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);
  ///Get the default microphone
  Mic := TCaptureDeviceManager.Current.DefaultAudioCaptureDevice;
  if Mic <> nil then
    //Set the SaveDialog filter to choose only the supported extension  
    SaveDialog1.Filter :=  Mic.FilterString;
    if SaveDialog1.Execute then
      RecordButton.Enabled := false;
      StopButton.Enabled := true;
      //Gets the name of the file where to save the recorded data
      Mic.FileName := SaveDialog1.FileName;
    ShowMessage('Audio capturing device not available');
5. Double-click the Stop button to attach OnClick event handlers to the Stop button:
procedure TForm1.StopButtonClick(Sender: TObject);
  if (Mic <> nil) and (Mic.State = TCaptureDeviceState.Capturing) then
    Image1.Opacity := 0;
    StopButton.Enabled := false;
    RecordButton.Enabled := true;

Run the Application

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



See Also