Add playSounds support (#43)

pull/52/head
Mattia Iavarone 7 years ago committed by GitHub
parent cf3bfc1ed0
commit f8d38e3c8c
  1. 19
      README.md
  2. 9
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java
  3. 53
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java
  4. 2
      cameraview/src/main/res/values/attrs.xml

@ -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|
@ -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
@ -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.

@ -93,6 +93,7 @@ public class CameraViewTest extends BaseTest {
// 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);
@ -449,6 +450,14 @@ public class CameraViewTest extends BaseTest {
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);

@ -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
@ -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;
@ -91,6 +93,7 @@ public class CameraView extends FrameLayout {
// 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()));
@ -130,6 +133,7 @@ public class CameraView extends FrameLayout {
// Apply self managed
setCropOutput(cropOutput);
setJpegQuality(jpegQuality);
setPlaySounds(playSounds);
// Apply camera controller params
setFacing(facing);
@ -1083,8 +1087,8 @@ public class CameraView extends FrameLayout {
* @see #captureSnapshot()
*/
public void capturePicture() {
if (mCameraController.capturePicture() && mUseSounds) {
// TODO: sound
if (mCameraController.capturePicture() && mPlaySounds) {
// TODO: playSound on Camera2
}
}
@ -1100,9 +1104,9 @@ public class CameraView extends FrameLayout {
* @see #capturePicture()
*/
public void captureSnapshot() {
if (mCameraController.captureSnapshot() && mUseSounds) {
if (mCameraController.captureSnapshot() && mPlaySounds) {
//noinspection all
sound(MediaActionSound.SHUTTER_CLICK);
playSound(MediaActionSound.SHUTTER_CLICK);
}
}
@ -1243,19 +1247,42 @@ public class CameraView extends FrameLayout {
//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();
@ -1436,9 +1463,9 @@ public class CameraView extends FrameLayout {
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) {

@ -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" />

Loading…
Cancel
Save