How Can I Record an Audio with Android API Audiorecorder In Delphi Pascal

Asked

Viewed 163 times

1

I have a small project where I need to record an audio by the android API through Delphi with Pascal, but I haven’t had much success in my codes. Could someone help me in this dilemma?

what I have is a mix of some examples I found on the internet and this so:

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 point is I don’t know if I should use this function in a loop or timer...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.