用手电筒播放morsecode?

最大B .:

我正在尝试构建一个摩尔斯电码应用程序,该应用程序可以使用内置的手电筒重播摩尔斯电文。因此,我尝试了一些方法,其中一些方法有些奏效,但没有怎么做。

因此,基本上,我键入一条消息,例如“ 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,但是效果也不理想。我只闪了一下,然后停了下来。

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

Gnk:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章