简体   繁体   English

如何使用JNI(Delphi)实例化java对象

[英]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. 我对Delphi中的编程还很陌生,我对Java一无所知。 However, I need to write a program using RAD Studio that can read the amplitude of an audio input. 但是,我需要使用可以读取音频输入幅度的RAD Studio编写程序。 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. 我无法在Delphi中找到一种方法(我能负担得起),但我确实找到了使用Android API的方法。 The solution looks to be in the MediaRecorder object so I tried to use it with this code. 解决方案看起来在MediaRecorder对象中,所以我尝试将它与此代码一起使用。

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. 但是, Recorder:= new MediaRecorder() (android想要它的方式)不起作用, Recorder:= MediaRecorder.Create (我认为Delphi通常想要它的方式)如果有人知道如何获取对象的实例或者知道从Delphi / RAD Studio获取音频振幅的更简单方法,请告诉我。

要在Delphi中创建Java类对象实例,在您的情况下是实现JMediaRecorder接口的类的实例,请使用以下代码:

Recorder := TJMediaRecorder.JavaClass.init;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM