所以,我试图让手机上的手电筒/手电筒闪烁或相对快速地闪烁。 我已将Cordova手电筒插件添加到我的应用程序中,它似乎工作正常,但当我尝试快速切换火炬时,没有任何变化。它似乎限于500毫秒的延迟或类似的东西。 我目前正在测试运行android 4.4.4的Galaxy S4 我已经安装了其他手电筒应用程序,并试图快速打开和关闭它,它工作正常,所以我知道硬件能够更快地闪烁。
我查看了插件的源代码,但我似乎无法找到任何可以覆盖或更改以解决问题的内容。也许我只是错过了它。
任何人都有任何关于如何覆盖可能存在的延迟的想法? 或者,如果有人有任何其他方法来实现这一点,我也会对这些方法感兴趣。
由于
JS:
$(document).ready(function(){
document.addEventListener("deviceready", function() {
setInterval(function () {
window.plugins.flashlight.toggle();
}, 200);
});
});
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
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();
}
}
答案 0 :(得分:0)
你提出的建议并不顺利,因为手电筒是Android中较为分散的方面之一,即使你没有使用Cordova,许多手机根本无法满足你的要求并且完全是原生的。
我曾经看过手机,例如,需要将近2秒的时间才能打开和关闭灯光,这将使你的效果无法实现。