[英]Change blink speed for Cordova flashlight plugin
因此,我試圖使手機上的手電筒/手電筒閃爍或閃爍得相對較快。 我已將Cordova手電筒插件添加到我的應用程序中,並且似乎可以正常工作,但是當我嘗試將手電筒的切換速度超過500ms時,沒有任何變化。 似乎僅限於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();
}
}
您提出的建議不能很好地工作,因為手電筒是Android較為零散的方面之一,而且即使您沒有使用Cordova且手機充裕,許多手機也無法滿足您的要求本機。
我見過一些手機,例如,需要近2秒鍾才能打開和關閉燈,這將使您的效果無法實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.