Search code examples
javaandroiddelphiaudioamplitude

How do I instantiate a java object using JNI (Delphi)


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.


Solution

  • 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;