Sounds are in wave format and in digital form sound has its volume change in timeline. In C++ Builder it is easy to use recording devices on Windows. To record a sound in Multi-Device applications we must use FMX.Media.hpp header.
1. Let’s create a new MultiDevice C++ Builder Project, save all project and unit files to a folder.
2. To define Audio Capture Device we need FMX.Media library. Go to Unit1.h header file add
1 2 3 |
#include <FMX.Media.hpp> |
and lets define Mic device
1 2 3 4 5 6 7 8 9 |
class TForm1 : public TForm { ... public: TAudioCaptureDevice *Mic; } |
3. Let’s back to Unit1.cpp codes, now we can define our Mic device as below
1 2 3 |
Mic =TCaptureDeviceManager::Current->DefaultAudioCaptureDevice; |
4. Now we can define FileName directly
1 2 3 |
Mic->FileName = L"D:\\test.wav"; |
or you can obtain file name by using SaveDialog component as below;
1 2 3 4 5 6 7 |
if(Mic!=NULL) { SaveDialog1->Filter = Mic->FilterString; Mic->FileName = SaveDialog1->FileName; } |
5. Add a Button now we can start recording device by using StartCapture() as below;
1 2 3 4 5 6 |
void __fastcall TForm1::Button1Click(TObject *Sender) { if(Mic!=NULL) Mic->StartCapture(); } |
6. Add another button to stop recording, and stop it as below;
1 2 3 4 5 6 |
void __fastcall TForm1::Button2Click(TObject *Sender) { if(Mic!=NULL) Mic->StopCapture(); } |
7. That’s All. Now you can run your codes by pressing F9.
Let’s look at all codes. Unit.h header file should be like this (FMX.Media library is needed);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
//--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <System.Classes.hpp> #include <FMX.Controls.hpp> #include <FMX.Forms.hpp> #include <FMX.Controls.Presentation.hpp> #include <FMX.StdCtrls.hpp> #include <FMX.Types.hpp> #include <FMX.Media.hpp> //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TButton *Button1; TButton *Button2; void __fastcall Button1Click(TObject *Sender); void __fastcall Button2Click(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); TAudioCaptureDevice *Mic; }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif |
Unit.cpp file should be like this;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <fmx.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.fmx" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { Mic =TCaptureDeviceManager::Current->DefaultAudioCaptureDevice; Mic->FileName = L"D:\\test.wav"; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { if (Mic!=NULL) { Mic->StartCapture(); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { if (Mic!=NULL) { Mic->StopCapture(); } } |