Skip to content

Commit

Permalink
Use same method to play sounds for consistency and so it works across…
Browse files Browse the repository at this point in the history
… multiple devices.
  • Loading branch information
cristianocca committed Mar 14, 2020
1 parent 1f89548 commit 0911852
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
21 changes: 20 additions & 1 deletion android/src/main/java/com/google/android/cameraview/Camera1.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.media.MediaActionSound;
import android.os.Build;
import android.os.Handler;
import androidx.collection.SparseArrayCompat;
Expand Down Expand Up @@ -82,6 +83,9 @@ class Camera1 extends CameraViewImpl implements MediaRecorder.OnInfoListener,

Camera mCamera;

// do not instantiate this every time since it allocates unnecessary resources
MediaActionSound sound = new MediaActionSound();

private Camera.Parameters mCameraParameters;

private final Camera.CameraInfo mCameraInfo = new Camera.CameraInfo();
Expand Down Expand Up @@ -752,6 +756,10 @@ public void onPictureTaken(byte[] data, Camera camera) {
// this shouldn't be needed and messes up autoFocusPointOfInterest
// camera.cancelAutoFocus();

if(mPlaySoundOnCapture){
sound.play(MediaActionSound.SHUTTER_CLICK);
}

if (options.hasKey("pauseAfterCapture") && !options.getBoolean("pauseAfterCapture")) {
camera.startPreview();
mIsPreviewActive = true;
Expand Down Expand Up @@ -1434,10 +1442,21 @@ private void setPlaySoundInternal(boolean playSoundOnCapture){
mPlaySoundOnCapture = playSoundOnCapture;
if(mCamera != null){
try{
mCamera.enableShutterSound(mPlaySoundOnCapture);
// Always disable shutter sound, and play our own.
// This is because not all devices honor this value when set to true
boolean res = mCamera.enableShutterSound(false);

// if we fail to disable the shutter sound
// set mPlaySoundOnCapture to false since it means
// we cannot change it and the system will play it
// playing the sound ourselves also makes it consistent with Camera2
if(!res){
mPlaySoundOnCapture = false;
}
}
catch(Exception ex){
Log.e("CAMERA_1::", "setPlaySoundInternal failed", ex);
mPlaySoundOnCapture = false;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ public void onImageAvailable(ImageReader reader) {

CameraDevice mCamera;

MediaActionSound sound = new MediaActionSound();

CameraCaptureSession mCaptureSession;

CaptureRequest.Builder mPreviewRequestBuilder;
Expand Down Expand Up @@ -1299,7 +1301,6 @@ public void onCaptureCompleted(@NonNull CameraCaptureSession session,
unlockFocus();
}
if (mPlaySoundOnCapture) {
MediaActionSound sound = new MediaActionSound();
sound.play(MediaActionSound.SHUTTER_CLICK);
}
}
Expand Down

0 comments on commit 0911852

Please sign in to comment.