简体   繁体   English

更改Cordova手电筒插件的闪烁速度

[英]Change blink speed for Cordova flashlight plugin

So, I am trying to make the torch/flashlight on my phone flicker or blink relatively quickly. 因此,我试图使手机上的手电筒/手电筒闪烁或闪烁得相对较快。 I have added the Cordova flashlight plugin to my app and it seems to be working alright but when I try to toggle the torch faster than 500ms nothing changes. 我已将Cordova手电筒插件添加到我的应用程序中,并且似乎可以正常工作,但是当我尝试将手电筒的切换速度超过500ms时,没有任何变化。 It seems to be limited to a 500ms delay or something of the sort. 似乎仅限于500毫秒的延迟或类似的延迟。 I am currently testing on a Galaxy S4 running android 4.4.4 I have installed other flashlight apps and have tried turning it on and off rapidly and it works fine so I know the hardware is capable of blinking faster. 我目前正在运行Android 4.4.4的Galaxy S4上进行测试,我已经安装了其他手电筒应用程序,并尝试快速打开和关闭它,并且效果很好,因此我知道硬件能够更快地闪烁。

I have looked through the source code for the plugin and I can't seem to find anything that I could override or change to fix the issue. 我已经查看了该插件的源代码,但似乎找不到可以覆盖或更改以解决该问题的任何内容。 Maybe I'm just missing it. 也许我只是想念它。

Anyone have any ideas of how I might override whatever delay might be in place? 任何人都对如何克服可能出现的延迟有任何想法? Or if anyone has any other methods to accomplish this I would be interested in those as well. 或者,如果有人有其他方法可以做到这一点,我也会对此感兴趣。

Thanks 谢谢

js: js:

$(document).ready(function(){
  document.addEventListener("deviceready", function() {
    setInterval(function () {
      window.plugins.flashlight.toggle();
    }, 200);
  });
});

flashlight.js flashlight.js

function Flashlight() {
  // track flashlight state
  this._isSwitchedOn = false;
}

Flashlight.prototype = {

  available: function (callback) {
    cordova.exec(function (avail) {
      callback(avail ? true : false);
    }, null, "Flashlight", "available", []);
  },

  switchOn: function (successCallback, errorCallback) {
    this._isSwitchedOn = true;
    cordova.exec(successCallback, errorCallback, "Flashlight", "switchOn", []);
  },

  switchOff: function (successCallback, errorCallback) {
    this._isSwitchedOn = false;
    cordova.exec(successCallback, errorCallback, "Flashlight", "switchOff", []);
  },

  toggle: function (successCallback, errorCallback) {
    if (this._isSwitchedOn) {
      this.switchOff(successCallback, errorCallback);
    } else {
      this.switchOn(successCallback, errorCallback);
    }
  }
};

Flashlight.install = function () {
  if (!window.plugins) {
    window.plugins = {};
  }

  window.plugins.flashlight = new Flashlight();
  return window.plugins.flashlight;
};

cordova.addConstructor(Flashlight.install);

flashlight.java flashlight.java

package nl.xservices.plugins;

import android.content.pm.FeatureInfo;
import android.content.pm.PackageManager;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Build;
import android.util.Log;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

public class Flashlight extends CordovaPlugin {

  private static final String ACTION_AVAILABLE = "available";
  private static final String ACTION_SWITCH_ON = "switchOn";
  private static final String ACTION_SWITCH_OFF = "switchOff";

  private static Boolean capable;
  private boolean releasing;
  private Camera mCamera;

  @Override
  public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    Log.d("Flashlight", "Plugin Called: " + action);
    try {
      if (action.equals(ACTION_SWITCH_ON)) {
        // When switching on immediately after checking for isAvailable,
        // the release method may still be running, so wait a bit.
        while (releasing) {
          Thread.sleep(10);
        }
        mCamera = Camera.open();
        if (Build.VERSION.SDK_INT >= 11) { // honeycomb
          // required for (at least) the Nexus 5
          mCamera.setPreviewTexture(new SurfaceTexture(0));
        }
        toggleTorch(true, callbackContext);
        return true;
      } else if (action.equals(ACTION_SWITCH_OFF)) {
        toggleTorch(false, callbackContext);
        releaseCamera();
        return true;
      } else if (action.equals(ACTION_AVAILABLE)) {
        if (capable == null) {
          mCamera = Camera.open();
          capable = isCapable();
          releaseCamera();
        }
        callbackContext.success(capable ? 1 : 0);
        return true;
      } else {
        callbackContext.error("flashlight." + action + " is not a supported function.");
        return false;
      }
    } catch (Exception e) {
      callbackContext.error(e.getMessage());
      return false;
    }
  }

  private boolean isCapable() {
    final PackageManager packageManager = this.cordova.getActivity().getPackageManager();
    for (final FeatureInfo feature : packageManager.getSystemAvailableFeatures()) {
      if (PackageManager.FEATURE_CAMERA_FLASH.equalsIgnoreCase(feature.name))     {
        return true;
      }
    }
    return false;
  }

  private void toggleTorch(boolean switchOn, CallbackContext callbackContext) {
    final Camera.Parameters mParameters = mCamera.getParameters();
    if (isCapable()) {
      mParameters.setFlashMode(switchOn ? Camera.Parameters.FLASH_MODE_TORCH : Camera.Parameters.FLASH_MODE_OFF);
      mCamera.setParameters(mParameters);
      mCamera.startPreview();
      callbackContext.success();
    } else {
      callbackContext.error("Device is not capable of using the flashlight. Please test with flashlight.available()");
    }
  }

  private void releaseCamera() {
    releasing = true;
    // we need to release the camera, so other apps can use it
    new Thread(new Runnable() {
      public void run() {
        mCamera.setPreviewCallback(null);
        mCamera.stopPreview();
        mCamera.release();
        releasing = false;
      }
    }).start();
  }
}

What you propose is not going to work well, as the flashlight is one of the more fragmented aspects of Android, and many phones simply won't be able to keep up with your requirements, even if you weren't using Cordova and were full native. 您提出的建议不能很好地工作,因为手电筒是Android较为零散的方面之一,而且即使您没有使用Cordova且手机充裕,许多手机也无法满足您的要求本机。

I have seen phones that, for instance, require almost 2 seconds to turn the light on and off, which will make your effect impossible. 我见过一些手机,例如,需要近2秒钟才能打开和关闭灯,这将使您的效果无法实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM