问题描述
首先,我习惯于使用Java进行编程,这就是为什么我使用Codename One开发移动应用程序的原因。
First off I am used to programming with Java that's why I am using Codename One to develop mobile applications.
但是有时我会看到一些我对测试感兴趣的Android格式代码。由于。
However sometimes I see some code in Android "format" that I am interested on testing. I know how to set up basic native interface thanks to Codename one tutorial.
例如,我想测试此。但是,它涉及使用此方法中可用的数据初始化Android onCreate()
方法中的某些变量,例如 am =(AudioManager)this.getSystemService (Context.AUDIO_SERVICE);
使用 this
,在代号一个本机接口中引用不相同。也许我不必使用 onCreate()
方法(可以),但我不是Android专家(也不是CN1专家!),所以我也不知道。
For example, I would like to test this snippet about real time sound processing. However it involves initializing some variable in the Android onCreate()
method with data that is available in this method such as am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
with the use of this
which has not the same reference in the Codename One Native Interface. Maybe I don't have to use the onCreate()
method (which could be reached from Codename One) but I am not an Android guru (nor a CN1 one either!), so I don't know.
因此,要在Codename One本机界面中测试本机Android代码,我必须进行哪些更改?
Consequently what changes do I have to make to test native Android code in Codename One native interface ? Maybe there is a methodology that I would be glad to hear of.
已解决:在本机接口实现中使用的代码有效
这里是的代号一个本机接口实现。 。确实没有使用Android onCreate()
方法,但是其中初始化的内容已经在 initRecordAndTrack()$ c $中转移了c>在以Codename One的形式适合时调用的方法。它使用与@akash所述相同的方法,因此将
this
替换为 com.codename1.impl.android.AndroidNativeUtil.getActivity()
。
Here is the Codename One native interface implementation of the original Android code. Indeed Android onCreate()
method has not been used but the things that were initialized in it have been transfered in the initRecordAndTrack()
method which is called when suited in the Codename One's form. It uses the same method as described below by @akash thus replacing this
by com.codename1.impl.android.AndroidNativeUtil.getActivity()
.
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.media.MediaPlayer;
import android.content.Context;
public class KestudisNativeInterfaceImpl {
boolean isRecording = false;
AudioManager am = null;
AudioRecord record = null;
AudioTrack track = null;
public void initRecordAndTrack() {
android.app.Activity ctx = com.codename1.impl.android.AndroidNativeUtil.getActivity();
am = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
am.setSpeakerphoneOn(true);
int min = AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
record = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION, 8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,
min);
// if (AcousticEchoCanceler.isAvailable())
// {
// AcousticEchoCanceler echoCancler = AcousticEchoCanceler.create(record.getAudioSessionId());
// echoCancler.setEnabled(true);
// }
int maxJitter = AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
track = new AudioTrack(AudioManager.MODE_IN_COMMUNICATION, 8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, maxJitter,
AudioTrack.MODE_STREAM);
(new Thread() {
@Override
public void run() {
recordAndPlay();
}
}).start();
}
public void startRecordAndPlay() {
record.startRecording();
track.play();
isRecording = true;
}
public void stopRecordAndPlay() {
record.stop();
track.pause();
isRecording = false;
}
private void recordAndPlay() {
short[] lin = new short[1024];
int num = 0;
am.setMode(AudioManager.MODE_IN_COMMUNICATION);
while (true) {
if (isRecording) {
num = record.read(lin, 0, 1024);
track.write(lin, 0, num);
}
}
}
public boolean isSupported() {
return true;
}
}
非常感谢,
干杯
推荐答案
看看这个链接
在android this
中通常引用上下文,并且要访问CN1中的上下文,可以将 this
替换为 com.codename1.impl.android.AndroidNativeUtil.getActivity()
In android this
normally refer to context and to access context in CN1 you can replace this
with com.codename1.impl.android.AndroidNativeUtil.getActivity()
希望有帮助
这篇关于如何使用Codename One测试Android本机代码段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!