4

I am still pretty new to programming in Delphi and I don't know anything about Java. However, I need to write a program using RAD Studio that can read the amplitude of an audio input. I was unable to find a way to do this in Delphi(That I could afford) but I did find a way using the Android API. The solution looks to be in the MediaRecorder object so I tried to use it with this code.

var
  Form1: TForm1;
  RecorderObj: Jobject;
  Recorder: JMediaRecorder;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Recorder:= MediaRecorder;  // <------- The problem is here.
  Recorder.setAudioSource(1);  // should set the recording device to the mic
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Label1.Text:= IntToStr(Recorder.getMaxAmplitude);
end;

It looks to me that this code doesn't work because there is no instance of the object. However, Recorder:= new MediaRecorder() (the way android wants it) doesn't work and neither does Recorder:= MediaRecorder.Create (the way I think Delphi usually wants it) If anyone knows how to get an instance of the object or knows an easier way to get audio amplitude from Delphi/RAD Studio please let me know.

Michael
  • 175
  • 1
  • 6

1 Answers1

2

To create a Java class object instance in Delphi, in your case an instance of a class that implements the JMediaRecorder interface, use the following code:

Recorder := TJMediaRecorder.JavaClass.init;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Dalija Prasnikar
  • 27,212
  • 44
  • 82
  • 159
  • 2
    I've also seen syntax like `Recorder := TJMediaRecorder.Create;` that might also work in some versions of Delphi. But, in general, calling `JavaClass.init()` is calling the actual Java class constructor. If a Java class constructor takes any parameters, so will `init()` to match. – Remy Lebeau Aug 09 '17 at 21:01