用手电筒重播摩尔斯电码?

replay morsecode with flashlight?

我正在尝试构建一个摩尔斯电码应用程序,它可以使用内置的手电筒重播摩尔斯电码。所以我尝试了一些东西,其中一个有点奏效,但不是应该的。

基本上我输入了一条消息,比方说 "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();
}