用手电筒重播morsecode?

时间:2018-10-29 17:13:59

标签: java android android-camera2 flashlight morse-code

我正在尝试构建一个莫尔斯电码应用程序,该应用程序可以使用内置的Flashlight播放莫尔斯电码。因此,我尝试了一些方法,其中一些方法稍有奏效,但未达到应有的水平。

因此,基本上我输入一条消息,比如说“你好”。转换为

.... . .-.. .-.. ---

然后我想通过点击一个按钮来重播。 我尝试了不同的事情。这是我的第一次尝试:

 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,但是效果也不理想。我只闪了一下,然后停了下来。

如您所见,我还没有经历过'^^ 希望您能够帮助我。预先感谢!

1 个答案:

答案 0 :(得分:1)

使用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();
}