开发者

OnFlashlight in android

In my android application i am recording video using Media recorder.I would like to on and off the flash as the recording is started and stopped. Is there anyway that i can achieve it in a开发者_开发百科ndroid.

Please share your valuable suggestions.

Thanks in advance :)


Have a look at FLASH_MODE_TORCH.

The whole code may goes like

Camera.Parameters parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);


When you are starting recoding , just call a function

/* * Turning On flash */ private void turnOnFlash() { if (!isFlashOn) { if (camera == null || params == null) { return; } // play sound playSound();

        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(params);
        camera.startPreview();
        isFlashOn = true;

        // changing button/switch image
        toggleButtonImage();
    }

}

and when you click on stop Recording just call Stop Flash Function

Flashlight can be turned off by setting flash mode to FLASH_MODE_OFF.

 /*
 * Turning Off flash
 */
private void turnOffFlash() {
    if (isFlashOn) {
        if (camera == null || params == null) {
            return;
        }
        // play sound
        playSound();

        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(params);
        camera.stopPreview();
        isFlashOn = false;

        // changing button/switch image
        toggleButtonImage();
    }
}

/* * Switch click event to toggle flash on/off */ btnSwitch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (isFlashOn) { // turn off flash turnOffFlash(); } else { // turn on flash turnOnFlash(); } } });

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜