Overview
A camera app often needs light: a security camera in a dark room, a scanner, a video call at night. KlugTorch switches the camera light (the LED next to the back camera) on and off while the camera keeps running.
KlugTorch first detects whether the device has a camera light at all. Many tablets and some phones have none, so an app can simply hide its light button there.
It works alongside a live camera stream. KlugTorch does not open a camera of its own, so it never competes with the stream for the camera.
Apps that stream with KlugMedia can also use its built-in torch control. KlugTorch is in daily use in our apps, our proof of concept.
Features
- Self-contained: one ANE, no core library or dependency ANEs to add
- Detects whether the device has a camera light
- Turn the camera light on and off with one call
- Works while the camera is streaming on iOS, no interruption
- Reads the current state of the light
- A change event after every switch, also when it fails
- One API for iOS and Android
Supported platforms
On devices without a camera light, and on other platforms, isSupported returns false, so the same code can stay in every app.
Getting started
Add the extension to your application descriptor:
<extensions>
<extensionID>com.klug.torch.Torch</extensionID>
</extensions>
The light sits next to the back camera, so the back camera should be the active one.
Code examples
Three complete flows. on() and off() return at once; a change event follows each call, and mode tells you the result. Full documentation →
import com.klug.torch.Torch; import flash.events.Event; var torch:Torch = Torch.getInstance(); // iOS: true if the default (back) camera has a torch. // Android 6+: true if a back-facing camera has a flash. // False on devices without a light and on other platforms. lightButton.visible = torch.isSupported; // lightButton: your app's toggle button
var wantLight:Boolean = false; torch.addEventListener( Event.CHANGE, onTorchChange ); // after every on() / off() function setLight( on:Boolean ):void { wantLight = on; if ( on ) torch.on(); else torch.off(); } function onTorchChange( e:Event ):void { // the event carries no detail: read mode for the current state lightButton.selected = torch.mode; if ( torch.mode != wantLight ) // the light could not be switched showMessage( "The light is not available right now." ); // your app }
function stopStreaming():void { if ( torch.mode ) setLight( false ); // first the light, then the camera stopCamera(); // your app's stream shutdown torch.removeEventListener( Event.CHANGE, onTorchChange ); }
API overview
| Torch.getInstance() | Returns the single instance. |
| isSupported | True only if the device has a usable camera light: on iOS the default (back) camera has a torch, on Android 6+ a back-facing camera has a flash. |
| on() | Turns the camera light on; a change event follows. |
| off() | Turns the camera light off; a change event follows. |
| mode | True while the light is on. iOS reads the camera's torch state; Android reports the state of the last successful on() / off(). |
Events
| Event.CHANGE ("change") | Dispatched after every on() and off(), also when the light could not be switched. Read mode for the result. |
Buy KlugTorch
A yearly licence for one AIR application, with all updates for 12 months. After payment you receive the extension, the ActionScript library and the documentation by e-mail, usually within one business day. Licence and terms
- Licence for one AIR application
- All updates for 12 months
- Answer by e-mail within 7 days
- Licence for one AIR application
- All updates for 12 months
- Answer by e-mail within 48 hours
Requirements
| AIR SDK | 51.x (HARMAN) |
| iOS | iOS 17 or later |
| Android | Android 6.0 (API 23) or later |
| Permissions | None of its own |
| Hardware | A camera light next to the back camera |