Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

Commit

Permalink
feat: [Android] add playSoundOnRecord (#3065)
Browse files Browse the repository at this point in the history
* add playSoundOnRecord

* fix missing functions

Co-authored-by: Tuan Luong <tuanluong@Tuans-MacBook-Pro.local>
Co-authored-by: Mateus Andrade <15278828+MateusAndrade@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 8, 2021
1 parent 5b318c1 commit 19d6865
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 0 deletions.
19 changes: 19 additions & 0 deletions android/src/main/java/com/google/android/cameraview/Camera1.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ class Camera1 extends CameraViewImpl implements MediaRecorder.OnInfoListener,

private Boolean mPlaySoundOnCapture = false;

private Boolean mPlaySoundOnRecord = false;

private boolean mustUpdateSurface;
private boolean surfaceWasDestroyed;

Expand Down Expand Up @@ -852,6 +854,9 @@ boolean record(String path, int maxDuration, int maxFileSize, boolean recordAudi
int deviceOrientation = displayOrientationToOrientationEnum(mDeviceOrientation);
mCallback.onRecordingStart(path, mOrientation != Constants.ORIENTATION_AUTO ? mOrientation : deviceOrientation, deviceOrientation);

if (mPlaySoundOnRecord) {
sound.play(MediaActionSound.START_VIDEO_RECORDING);
}

return true;
} catch (Exception e) {
Expand Down Expand Up @@ -1534,6 +1539,16 @@ public boolean getPlaySoundOnCapture(){
return mPlaySoundOnCapture;
}

@Override
void setPlaySoundOnRecord(boolean playSoundOnRecord) {
mPlaySoundOnRecord = playSoundOnRecord;
}

@Override
boolean getPlaySoundOnRecord() {
return mPlaySoundOnRecord;
}

@Override
public void onPreviewFrame(byte[] data, Camera camera) {
Camera.Size previewSize = mCameraParameters.getPreviewSize();
Expand Down Expand Up @@ -1600,6 +1615,10 @@ private void stopMediaRecorder() {

mCallback.onRecordingEnd();

if (mPlaySoundOnRecord) {
sound.play(MediaActionSound.STOP_VIDEO_RECORDING);
}

int deviceOrientation = displayOrientationToOrientationEnum(mDeviceOrientation);

if (mVideoPath == null || !new File(mVideoPath).exists()) {
Expand Down
18 changes: 18 additions & 0 deletions android/src/main/java/com/google/android/cameraview/Camera2.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ public void onImageAvailable(ImageReader reader) {

private Boolean mPlaySoundOnCapture = false;

private Boolean mPlaySoundOnRecord = false;

private Surface mPreviewSurface;

private Rect mInitialCropRegion;
Expand Down Expand Up @@ -598,6 +600,9 @@ boolean record(String path, int maxDuration, int maxFileSize, boolean recordAudi
// same TODO as onVideoRecorded
mCallback.onRecordingStart(mVideoPath, 0, 0);

if (mPlaySoundOnRecord) {
sound.play(MediaActionSound.START_VIDEO_RECORDING);
}
return true;
} catch (CameraAccessException | IOException e) {
e.printStackTrace();
Expand Down Expand Up @@ -709,6 +714,16 @@ public boolean getPlaySoundOnCapture(){
return mPlaySoundOnCapture;
}

@Override
void setPlaySoundOnRecord(boolean playSoundOnRecord) {
mPlaySoundOnRecord = playSoundOnRecord;
}

@Override
boolean getPlaySoundOnRecord() {
return mPlaySoundOnRecord;
}

@Override
void setScanning(boolean isScanning) {
if (mIsScanning == isScanning) {
Expand Down Expand Up @@ -1428,6 +1443,9 @@ private void stopMediaRecorder() {
mMediaRecorder = null;

mCallback.onRecordingEnd();
if (mPlaySoundOnRecord) {
sound.play(MediaActionSound.STOP_VIDEO_RECORDING);
}

if (mVideoPath == null || !new File(mVideoPath).exists()) {
// @TODO: implement videoOrientation and deviceOrientation calculation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ protected Parcelable onSaveInstanceState() {
state.zoom = getZoom();
state.whiteBalance = getWhiteBalance();
state.playSoundOnCapture = getPlaySoundOnCapture();
state.playSoundOnRecord = getPlaySoundOnRecord();
state.scanning = getScanning();
state.pictureSize = getPictureSize();
return state;
Expand All @@ -280,6 +281,7 @@ protected void onRestoreInstanceState(Parcelable state) {
setZoom(ss.zoom);
setWhiteBalance(ss.whiteBalance);
setPlaySoundOnCapture(ss.playSoundOnCapture);
setPlaySoundOnRecord(ss.playSoundOnRecord);
setScanning(ss.scanning);
setPictureSize(ss.pictureSize);
}
Expand Down Expand Up @@ -604,6 +606,14 @@ public boolean getPlaySoundOnCapture() {
return mImpl.getPlaySoundOnCapture();
}

public void setPlaySoundOnRecord(boolean playSoundOnRecord) {
mImpl.setPlaySoundOnRecord(playSoundOnRecord);
}

public boolean getPlaySoundOnRecord() {
return mImpl.getPlaySoundOnRecord();
}

public void setScanning(boolean isScanning) { mImpl.setScanning(isScanning);}

public boolean getScanning() { return mImpl.getScanning(); }
Expand Down Expand Up @@ -765,6 +775,8 @@ protected static class SavedState extends BaseSavedState {

boolean playSoundOnCapture;

boolean playSoundOnRecord;

boolean scanning;

Size pictureSize;
Expand All @@ -782,6 +794,7 @@ public SavedState(Parcel source, ClassLoader loader) {
zoom = source.readFloat();
whiteBalance = source.readInt();
playSoundOnCapture = source.readByte() != 0;
playSoundOnRecord = source.readByte() != 0;
scanning = source.readByte() != 0;
pictureSize = source.readParcelable(loader);
}
Expand All @@ -803,6 +816,7 @@ public void writeToParcel(Parcel out, int flags) {
out.writeFloat(zoom);
out.writeInt(whiteBalance);
out.writeByte((byte) (playSoundOnCapture ? 1 : 0));
out.writeByte((byte) (playSoundOnRecord ? 1 : 0));
out.writeByte((byte) (scanning ? 1 : 0));
out.writeParcelable(pictureSize, flags);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ abstract boolean record(String path, int maxDuration, int maxFileSize,

abstract boolean getPlaySoundOnCapture();

abstract void setPlaySoundOnRecord(boolean playSoundOnRecord);

abstract boolean getPlaySoundOnRecord();

abstract void setScanning(boolean isScanning);

abstract boolean getScanning();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ public void setPlaySoundOnCapture(RNCameraView view, boolean playSoundOnCapture)
view.setPlaySoundOnCapture(playSoundOnCapture);
}

@ReactProp(name = "playSoundOnRecord")
public void setPlaySoundOnRecord(RNCameraView view, boolean playSoundOnRecord) {
view.setPlaySoundOnRecord(playSoundOnRecord);
}

@ReactProp(name = "barCodeTypes")
public void setBarCodeTypes(RNCameraView view, ReadableArray barCodeTypes) {
if (barCodeTypes == null) {
Expand Down
4 changes: 4 additions & 0 deletions docs/RNCamera.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@ An `{width:, height: }` object which defines the width and height of the cameraV

Boolean to turn on or off the camera's shutter sound (default false). Note that in some countries, the shutter sound cannot be turned off.

### `Android` `playSoundOnRecord`

Boolean to turn on or off the camera's record sound (default false)

### `iOS` `videoStabilizationMode`

The video stabilization mode used for a video recording. The possible values are:
Expand Down
3 changes: 3 additions & 0 deletions src/RNCamera.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ type PropsType = typeof View.props & {
keepAudioSession?: boolean,
useCamera2Api?: boolean,
playSoundOnCapture?: boolean,
playSoundOnRecord?: boolean,
videoStabilizationMode?: number | string,
pictureSize?: string,
rectOfInterest: Rect,
Expand Down Expand Up @@ -442,6 +443,7 @@ export default class Camera extends React.Component<PropsType, StateType> {
keepAudioSession: PropTypes.bool,
useCamera2Api: PropTypes.bool,
playSoundOnCapture: PropTypes.bool,
playSoundOnRecord: PropTypes.bool,
videoStabilizationMode: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
pictureSize: PropTypes.string,
mirrorVideo: PropTypes.bool,
Expand Down Expand Up @@ -487,6 +489,7 @@ export default class Camera extends React.Component<PropsType, StateType> {
keepAudioSession: false,
useCamera2Api: false,
playSoundOnCapture: false,
playSoundOnRecord: false,
pictureSize: 'None',
videoStabilizationMode: 0,
mirrorVideo: false,
Expand Down
2 changes: 2 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ export interface RNCameraProps {
permissionDialogMessage?: string;
/** Android only */
playSoundOnCapture?: boolean;
/** Android only */
playSoundOnRecord?: boolean;

androidCameraPermissionOptions?: {
title: string;
Expand Down

0 comments on commit 19d6865

Please sign in to comment.