我正在尝试构建一个Morse-Code应用程序,该应用程序可以使用内置的Flashlight播放Morse。因此,我尝试了一些方法,其中一些方法有些奏效,但没有怎么做。
因此,基本上,我键入一条消息,例如“ hello”。它翻译成
.... . .-.. .-.. ---
然后,我想通过点击一个按钮来重播。
我尝试了不同的事情。这是我的第一次尝试:
public void onPlayflash(View view) throws InterruptedException, CameraAccessException {
if (result == null) {
output.setText("ERROR");
} else {
currentposition = 0;
if (currentposition < result.length()) {
String c = String.valueOf(result.charAt(0));
if (c.equals("-")) {
//timeinmillis = 1000;
//setTimer();
flash.setTorchMode(flash.getCameraIdList()[0], true);
Thread.sleep(2000);
flash.setTorchMode(flash.getCameraIdList()[0], false);
} else if (c.equals(".")) {
//timeinmillis = 500;
//setTimer();
flash.setTorchMode(flash.getCameraIdList()[0], true);
Thread.sleep(1000);
flash.setTorchMode(flash.getCameraIdList()[0], false);
} else {
Thread.sleep(2000);
}
currentposition += 1;
}
}
}
这没用。它只是说:
I/Choreographer: Skipped (*always a random number over 1000 here*) frames! The application may be doing too much work on its main thread.
然后我尝试
public void onPlayflash(View view) throws InterruptedException, CameraAccessException {
if (result == null) {
output.setText("ERROR");
} else {
for (int i = 0; i < result.length(); i++) {
String c = String.valueOf(result.charAt(i));
if (c.equals("_")) {
flash.setTorchMode(flash.getCameraIdList()[0], true);
Thread.sleep(2000);
flash.setTorchMode(flash.getCameraIdList()[0], false);
Thread.sleep(500);
} else if (c.equals(".")) {
flash.setTorchMode(flash.getCameraIdList()[0], true);
Thread.sleep(1000);
flash.setTorchMode(flash.getCameraIdList()[0], false);
Thread.sleep(500);
} else {
Thread.sleep(1500);
}
}
}
}
这有点奏效,但它仍然说
I/Choreographer: Skipped (*always a random number over 1000 here*) frames! The application may be doing too much work on its main thread.
实际上,重播可以很好地开始,但是随后开始变得困难,并跳过了部分迭代。
如您所见,我还尝试了android.os.CountDownTimer,但是效果也不理想。我只闪了一下,然后停了下来。
如您所见,我还没有经历过'^^
希望您能够帮助我。提前致谢!
最佳答案
使用CountDownTimer再试一次,但是使用递归并传递一个等待时间的列表
private void test(List<Integer> resultTimeList, Integer count) {
flash.setTorchMode(flash.getCameraIdList()[0], true);
new CountDownTimer(resultTimeList.get(count), 500) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
new CountDownTimer(500, 500) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
flash.setTorchMode(flash.getCameraIdList()[0], false);
test(resultTimeList, count++);
}
}.start();
}
}.start();
}