【问题标题】:replay morsecode with flashlight?用手电筒重放摩尔斯电码?
【发布时间】:2018-10-29 17:13:58
【问题描述】:

我正在尝试构建一个摩尔斯电码应用程序,它可以使用内置手电筒重播摩尔斯电码。所以我尝试了一些方法,其中一个稍微有效,但没有达到应有的效果。

所以基本上我输入了一条消息,让我们说“你好”。它转化为

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

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

 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,但也没有成功。我只闪了一下,然后就停了。

如你所见,我还没有那么有经验'^^ 希望您能够帮助我。提前致谢!

【问题讨论】:

  • 尝试让屏幕黑白闪烁。您可能会感到惊喜。

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


【解决方案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();
}

【讨论】:

  • 非常感谢!!我也是这样做的。我没有使用列表,而是使用了 HashMap(对我来说效果更好),参数的最终声明存在一些问题,但最终效果很好!我什至没有冻结!只有一个小问题:成功重播后应用程序崩溃,但我想我能解决这个问题。该死的感觉真好!
  • 实际上,你甚至不需要在 onFinish()-Method 中增加一个计时器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-23
  • 1970-01-01
相关资源
最近更新 更多