I have a small project where I need to record an audio through the android API through Delphi with Pascal, but I have not been very successful in my codes. Could someone help me in this dilemma?
What I have is a mixture of some examples I found on the internet and it looks like this:
procedure RecordAudioFile(NomeArquivo : String);
Var
AudioRecord: JAudioRecord;
M : TMemoryStream;
I : Integer;
TAmpOn : TJavaArray<SmallInt>;
Okunan : Integer;
Begin
AUDIO_CHANNELS := TJAudioFormat.JavaClass.CHANNEL_IN_MONO;
AUDIO_ENCODING := TJAudioFormat.JavaClass.ENCODING_PCM_16BIT;
RECORDER_AUDIO_SOURCE := TJMediaRecorder_AudioSource.JavaClass.MIC;
RECORDER_SAMPLERATE := 11025; //8000;
BufferElements2Rec := 1024 ; //want to play 2048 (2K) since 2 bytes we use only 1024
BytesPerElement := 2; // 2 bytes in 16bit format
AudioRecord := TJAudioRecord.JavaClass.init(RECORDER_AUDIO_SOURCE,RECORDER_SAMPLERATE,AUDIO_CHANNELS,AUDIO_ENCODING,BufferElements2Rec * BytesPerElement);
(AudioRecord As JAudioRecord).startRecording;
Okunan := 0;
MyBytes := TBytesStream.Create;
for I := 1 to 2 do
Begin
TAmpOn := TJavaArray<SmallInt>.Create(BufferElements2Rec);
Okunan := Okunan + (AudioRecord As JAudioRecord).read(TAmpOn,0,BufferElements2Rec);
MyBytes.Write(TAmpOn.Data^, TAmpOn.Length * BytesPerElement);
TAmpOn.Free;
End;
(AudioRecord As JAudioRecord).stop;
AudioRecord.release;
MyBytes.SaveToFile(GetPathFileName(NomeArquivo)); //GetPathFileName Retorna IncludeTrailingPathDelimiter(System.IOUtils.TPath.GetHomePath)+NomeArquivo
End;
The question is that I do not know if I should use this function in a loop or timer, I could not solve this question ...