Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- RequestUserPermission requestUserPermission = new RequestUserPermission(this);
- requestUserPermission.verifyStoragePermissions();
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
- createSoundPoolWithBuilder();
- } else {
- createSoundPoolWithConstructor();
- }
- soundPool.setOnLoadCompleteListener(this);
- sound = soundPool.load(this, R.raw.click, 1);
- mySwitch = (Switch) findViewById(R.id.my_switch);
- mySwitch.setChecked(false);
- mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
- if (isChecked) {
- setFlashLigthOn();
- } else {
- setFlashLightOff();
- }
- }
- });
- boolean isCameraFlash = getApplicationContext().getPackageManager()
- .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
- if (!isCameraFlash) {
- showCameraAlert();
- } else {
- camera = Camera.open();
- }
- }
- private void showCameraAlert() {
- new AlertDialog.Builder(this)
- .setTitle(R.string.error_title)
- .setMessage(R.string.error_text)
- .setPositiveButton(R.string.exit_message, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- finish();
- }
- })
- .setIcon(android.R.drawable.ic_dialog_alert)
- .show();
- }
- @TargetApi(Build.VERSION_CODES.LOLLIPOP)
- protected void createSoundPoolWithBuilder() {
- AudioAttributes attributes = new AudioAttributes.Builder()
- .setUsage(AudioAttributes.USAGE_GAME)
- .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
- .build();
- soundPool = new SoundPool.Builder().setAudioAttributes(attributes).setMaxStreams(1).build();
- }
- @SuppressWarnings("deprecation")
- protected void createSoundPoolWithConstructor() {
- soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
- }
- private void setFlashLigthOn() {
- soundPool.play(sound, 1, 1, 0, 0, 1);
- new Thread(new Runnable() {
- @Override
- public void run() {
- if (camera != null) {
- parameters = camera.getParameters();
- if (parameters != null) {
- List supportedFlashModes = parameters.getSupportedFlashModes();
- if (supportedFlashModes.contains(Parameters.FLASH_MODE_TORCH)) {
- parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
- // } else if (supportedFlashModes.contains(Parameters.FLASH_MODE_ON)) {
- // parameters.setFlashMode(Parameters.FLASH_MODE_ON);
- } else camera = null;
- if (camera != null) {
- try {
- camera.setParameters(parameters);
- camera.startPreview();
- } catch (RuntimeException f) {
- }
- try {
- camera.setPreviewTexture(new SurfaceTexture(0));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- }).start();
- }
- private void setFlashLightOff() {
- soundPool.play(sound, 1, 1, 0, 0, 1);
- new Thread(new Runnable() {
- @Override
- public void run() {
- if (camera != null) {
- parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
- camera.setParameters(parameters);
- camera.stopPreview();
- }
- }
- }).start();
- }
- private void releaseCamera() {
- if (camera != null) {
- camera.release();
- camera = null;
- }
- }
- @Override
- protected void onStop() {
- super.onStop();
- // releaseCamera();
- }
- @Override
- protected void onPause() {
- super.onPause();
- //releaseCamera();
- // mySwitch.setChecked(false);
- }
- @Override
- protected void onResume() {
- super.onResume();
- if (camera == null) {
- camera = Camera.open();
- } else {
- setFlashLigthOn();
- }
- // mySwitch.setChecked(false);
- }
- @Override
- protected void onStart() {
- super.onStart();
- camera = Camera.open();
- }
- @Override
- public void onLoadComplete(SoundPool soundPool, int i, int i1) {
- }
Add Comment
Please, Sign In to add comment