Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ Most camera parameters can be controlled through XML attributes or linked method
app:cameraVideoQuality="480p"
app:cameraWhiteBalance="auto"
app:cameraHdr="off"
app:cameraAudio="on"/>
app:cameraAudio="on"
app:cameraPlaySounds="true"/>
```

|XML Attribute|Method|Values|Default Value|
Expand All @@ -311,6 +312,7 @@ Most camera parameters can be controlled through XML attributes or linked method
|[`cameraWhiteBalance`](#camerawhitebalance)|`setWhiteBalance()`|`auto` `incandescent` `fluorescent` `daylight` `cloudy`|`auto`|
|[`cameraHdr`](#camerahdr)|`setHdr()`|`off` `on`|`off`|
|[`cameraAudio`](#cameraaudio)|`setAudio()`|`off` `on`|`on`|
|[`cameraPlaySounds`](#cameraplaysounds)|`setPlaySounds()`|`true` `false`|`true`|

#### cameraSessionType

Expand Down Expand Up @@ -408,13 +410,26 @@ cameraView.setHdr(Hdr.ON);

#### cameraAudio

Turns on or off audio stream.
Turns on or off audio stream while recording videos.

```java
cameraView.setAudio(Audio.OFF);
cameraView.setAudio(Audio.ON);
```

#### cameraPlaySounds

Controls whether we should play platform-provided sounds during certain events (shutter click, focus completed).
Please note that:

- on API < 16, this flag is always set to `false`
- the Camera1 engine will always play shutter sounds regardless of this flag

```java
cameraView.setPlaySounds(true);
cameraView.setPlaySounds(false);
```

## Other APIs

Other APIs not mentioned above are provided, and are well documented and commented in code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public void testDefaults() {
// Self managed
assertEquals(cameraView.getExposureCorrection(), 0f, 0f);
assertEquals(cameraView.getZoom(), 0f, 0f);
assertEquals(cameraView.getPlaySounds(), CameraView.DEFAULT_PLAY_SOUNDS);
assertEquals(cameraView.getCropOutput(), CameraView.DEFAULT_CROP_OUTPUT);
assertEquals(cameraView.getJpegQuality(), CameraView.DEFAULT_JPEG_QUALITY);
assertEquals(cameraView.getGestureAction(Gesture.TAP), GestureAction.DEFAULT_TAP);
Expand Down Expand Up @@ -449,6 +450,14 @@ public void testSetJpegQuality() {
assertEquals(cameraView.getJpegQuality(), 100);
}

@Test
public void testSetPlaySounds() {
cameraView.setPlaySounds(true);
assertEquals(cameraView.getPlaySounds(), true);
cameraView.setPlaySounds(false);
assertEquals(cameraView.getPlaySounds(), false);
}

@Test(expected = IllegalArgumentException.class)
public void testSetJpegQuality_illegal() {
cameraView.setJpegQuality(-10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ public class CameraView extends FrameLayout {

final static int DEFAULT_JPEG_QUALITY = 100;
final static boolean DEFAULT_CROP_OUTPUT = false;
final static boolean DEFAULT_PLAY_SOUNDS = true;

// Self managed parameters
private int mJpegQuality;
private boolean mCropOutput;
private float mZoomValue;
private float mExposureCorrectionValue;
private boolean mPlaySounds;
private HashMap<Gesture, GestureAction> mGestureMap = new HashMap<>(4);

// Components
Expand All @@ -60,7 +62,7 @@ public class CameraView extends FrameLayout {
private CameraController mCameraController;
private Preview mPreviewImpl;
private ArrayList<CameraListener> mListeners = new ArrayList<>(2);

private MediaActionSound mSound;

// Views
GridLinesLayout mGridLinesLayout;
Expand Down Expand Up @@ -91,6 +93,7 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
// Self managed
int jpegQuality = a.getInteger(R.styleable.CameraView_cameraJpegQuality, DEFAULT_JPEG_QUALITY);
boolean cropOutput = a.getBoolean(R.styleable.CameraView_cameraCropOutput, DEFAULT_CROP_OUTPUT);
boolean playSounds = a.getBoolean(R.styleable.CameraView_cameraPlaySounds, DEFAULT_PLAY_SOUNDS);

// Camera controller params
Facing facing = Facing.fromValue(a.getInteger(R.styleable.CameraView_cameraFacing, Facing.DEFAULT.value()));
Expand Down Expand Up @@ -130,6 +133,7 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
// Apply self managed
setCropOutput(cropOutput);
setJpegQuality(jpegQuality);
setPlaySounds(playSounds);

// Apply camera controller params
setFacing(facing);
Expand Down Expand Up @@ -1083,8 +1087,8 @@ public void clearCameraListeners() {
* @see #captureSnapshot()
*/
public void capturePicture() {
if (mCameraController.capturePicture() && mUseSounds) {
// TODO: sound
if (mCameraController.capturePicture() && mPlaySounds) {
// TODO: playSound on Camera2
}
}

Expand All @@ -1100,9 +1104,9 @@ public void capturePicture() {
* @see #capturePicture()
*/
public void captureSnapshot() {
if (mCameraController.captureSnapshot() && mUseSounds) {
if (mCameraController.captureSnapshot() && mPlaySounds) {
//noinspection all
sound(MediaActionSound.SHUTTER_CLICK);
playSound(MediaActionSound.SHUTTER_CLICK);
}
}

Expand Down Expand Up @@ -1243,19 +1247,42 @@ private void requestPermissions(boolean requestCamera, boolean requestAudio) {

//endregion

//region Callbacks and dispatching

private MediaActionSound mSound;
private final boolean mUseSounds = Build.VERSION.SDK_INT >= 16;
//region Sounds

@SuppressLint("NewApi")
private void sound(int soundType) {
if (mUseSounds) {
private void playSound(int soundType) {
if (mPlaySounds) {
if (mSound == null) mSound = new MediaActionSound();
mSound.play(soundType);
}
}

/**
* Controls whether CameraView should play sound effects on certain
* events (picture taken, focus complete). Note that:
* - On API level < 16, this flag is always false
* - Camera1 will always play the shutter sound when taking pictures
*
* @param playSounds whether to play sound effects
*/
public void setPlaySounds(boolean playSounds) {
mPlaySounds = playSounds && Build.VERSION.SDK_INT >= 16;
}

/**
* Gets the current sound effect behavior.
*
* @see #setPlaySounds(boolean)
* @return whether sound effects are supported
*/
public boolean getPlaySounds() {
return mPlaySounds;
}

//endregion

//region Callbacks and dispatching

interface CameraCallbacks extends OrientationHelper.Callbacks {
void dispatchOnCameraOpened(CameraOptions options);
void dispatchOnCameraClosed();
Expand Down Expand Up @@ -1436,9 +1463,9 @@ public void dispatchOnFocusEnd(@Nullable final Gesture gesture, final boolean su
mUiHandler.post(new Runnable() {
@Override
public void run() {
if (success && mUseSounds) {
if (success && mPlaySounds) {
//noinspection all
sound(MediaActionSound.FOCUS_COMPLETE);
playSound(MediaActionSound.FOCUS_COMPLETE);
}

if (gesture != null && mGestureMap.get(gesture) == GestureAction.FOCUS_WITH_MARKER) {
Expand Down
2 changes: 2 additions & 0 deletions cameraview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
<enum name="on" value="1" />
</attr>

<attr name="cameraPlaySounds" format="boolean" />

<!-- deprecated attr name="cameraZoomMode" format="enum">
<enum name="off" value="0" />
<enum name="pinch" value="1" />
Expand Down